【问题标题】:Storing data from ObjectiveFlickr in a Singleton on iPhone在 iPhone 上将来自 ObjectiveFlickr 的数据存储在单例中
【发布时间】:2011-05-27 21:20:42
【问题描述】:

目标是拥有一个名为 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 中实现这一点。

【问题讨论】:

    标签: iphone objective-c flickr


    【解决方案1】:

    返回的 NSDictionary 中有什么?您确定它们都是有效的 plist 对象吗?可能需要修改照片数据(例如,将 base 64 编码为数组),然后才能进行写入。

    NSDictionary writeToFile 的文档:说

    此方法在写出文件之前递归地验证所有包含的对象都是属性列表对象(NSData、NSDate、NSNumber、NSString、NSArray 或 NSDictionary 的实例),如果所有对象都不是属性列表对象,则返回 NO ,因为生成的文件不是有效的属性列表。

    至于单例方面 - 您一次会进行多个这样的调用吗?是否需要持久化数据?如果没有和没有,只需将字典保存在内存中。如果一次发生多个调用,则需要另一层抽象(一些索引)来将每个调用的结果放在它自己的唯一位置。您当前的实现不会发生这种情况。

    【讨论】:

    • 理想情况下,应用程序会将数据保存到应用程序沙箱中的磁盘。虽然这不需要单例,但它仍然需要持久性。
    • 这返回 no 所以我认为这就是答案。将尝试将图像数据保存到 disk.if([inResponseDictionary writeToFile:@"inResponseDictionary.xml" atomically:YES]) {NSLog(@"yes");} else { NSLog(@"no"); }
    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 2011-12-23
    • 2011-07-07
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多