开发者

Storing data from ObjectiveFlickr in a Singleton on iPhone

开发者 https://www.devze.com 2023-03-09 01:58 出处:网络
The goal is to have a singleton data controller class called FetchData.h/.mthat pulls data using ObjectiveFlickr ( https://github.com/lukhnos/objectiveflickr ).

The goal is to have a singleton data controller class called FetchData.h/.m that pulls data using ObjectiveFlickr ( https://github.com/lukhnos/objectiveflickr ).

FetchData.m grabs the data with this:

OFFlickrAPIContext *context = [[OFFlickrAPIContext alloc] initWithAPIKey:YOUR_KEY sharedSecret:YOUR_SHARED_SECRET];
OFFlickrAPIRequest *request = [[OFFlickrAPIRequest alloc] initWithAPIContext:context];

// set the delegate, here we assume it's the controller that's creating the request object
[request setDelegate:self];
[request callAPIMethodWithGET:@"flickr.photos.getRecent" arguments:[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"per_page", nil]]

and then implement the following delegate:

- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary;

Currently I have this code to save the NSDictionary as a property list to a file as an alternative to a singleton:

  - (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:     (NSDictionary *)inResponseDictionary{
if([inResponseDictionary writeToFile:@"inRe开发者_如何学JAVAsponseDictionary.xml" atomically:TRUE])
{
NSLog(@"%@", [[NSFileManager defaultManager] fileExistsAtPath:@"inResponseDictionary.xml"]);
}
}

When I read this file back, I get Null. The file is read back as such:

NSDictionary *inResponseDictionary = [NSDictionary dictionaryWithContentsOfFile:@"inResponseDictionary.xml"];
NSDictionary *photoDict = [[inResponseDictionary valueForKeyPath:@"photos.photo"] objectAtIndex:0];

NSLog(@"%@", [photoDict count]);

Is there a better way to store this data from ObjectiveFlickr so that it can be accessed by other classes and view controllers? Or is there a better way to implement this in the View Controller.


What is in the returned NSDictionary? Are you sure they are all valid plist objects? The photo data might need to be modified (say, base 64 encoded into an array) before your write will work.

The docs for NSDictionary writeToFile: say

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

As for the singleton aspect - will you be making more than one of these calls at a time? Is there a need to persist the data? If no & no, just keep the dictionary in memory. If you have multiple calls happening at once, you'll need another layer of abstraction (some indexing) to put each call's results in it's own unique location. And that's not happening with your current implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消