在iPhone上的Singleton中存储来自ObjectiveFlickr的数据

问题描述

|| 目标是创建一个名为FetchData.h / .m的单例数据控制器类,该类使用ObjectiveFlickr(https://github.com/lukhnos/objectiveflickr)提取数据。 FetchData.m使用以下方法获取数据:
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]]
然后实现以下委托:
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary;
目前,我有以下代码将NSDictionary作为属性列表保存到文件中,以替代单例:
  - (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:     (NSDictionary *)inResponseDictionary{
if([inResponseDictionary writetoFile:@\"inResponseDictionary.xml\" atomically:TRUE])
{
NSLog(@\"%@\",[[NSFileManager defaultManager] fileExistsAtPath:@\"inResponseDictionary.xml\"]);
}
}
当我读回该文件时,我得到Null。这样读取文件
NSDictionary *inResponseDictionary = [NSDictionary dictionaryWithContentsOfFile:@\"inResponseDictionary.xml\"];
NSDictionary *photoDict = [[inResponseDictionary valueForKeyPath:@\"photos.photo\"] objectAtIndex:0];

NSLog(@\"%@\",[photoDict count]);
有没有更好的方法来存储来自ObjectiveFlickr的数据,以便其他类和视图控制器可以访问它?还是有更好的方法在View Controller中实现这一点。     

解决方法

        返回的NSDictionary中包含什么?您确定它们都是有效的plist对象吗?在写操作生效之前,可能需要修改照片数据(例如,将base 64编码为数组)。 NSDictionary writeToFile的文档:说   此方法递归地验证所有包含的对象是属性列表对象(NSData,NSDate,NSNumber,NSString,NSString,NSArray或NSDictionary的实例),然后写出文件;如果所有对象都不是属性列表对象,则返回NO。结果文件将不是有效的属性列表。 至于单例方面-您一次要打多个电话吗?是否需要保留数据?如果否,则将字典保存在内存中。如果您一次发生多个调用,则需要另一层抽象(一些索引)来将每个调用的结果放置在其自己的唯一位置。而当前的实现并不会发生这种情况。