[英]Trouble Using NSKeyedUnarchiver during init
我創建了一個FacebookManager單例,該單例在我的應用程序啟動時在后台線程上被調用。 與單身人士,應用程序等Facebook經理一起,一切都可以正常工作。但是,當應用程序首次啟動時,它還需要幾秒鍾的時間,因為Facebook經理尚未完成其工作。 因此,我想做的是使用NSKeyedArchiver保存facebookManager及其所有字典,以便在啟動時,該應用程序具有可導航的界面,而Facebook數據在后台進行更新。 說得通?
首先在FacebookManager.m中,當經理完成更新朋友字典等后,我調用保存數據的方法:
- (BOOL)saveFacebookData
{
// returns success or failure
NSString *path = [self archivePath]; // just a helper method
return [NSKeyedArchiver archiveRootObject:self toFile:path];
}
然后在init中,我正在嘗試這種方法,但似乎不起作用。 :
-(id)init
{
self = [super init];
NSString *path = [self archivePath];
self = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// If the manager hadn't been saved previously, create a new new one
if (!self) {
if (_idsByNameDict == nil) {
_idsByNameDict = [[NSMutableDictionary alloc] init];
}
if (_photosByNameDict == nil) {
_photosByNameDict = [[NSMutableDictionary alloc] init];
}
if (_installedByNameDict == nil) {
_installedByNameDict = [[NSMutableDictionary alloc] init];
}
if (_allFriendsArray == nil) {
_allFriendsArray = [[NSArray alloc] init];
}
basicPermissions = NO;
extendedPermissions = NO;
// Create synchronous dispatch queue for all facebook activity
if (_facebookUpdateQueue == nil) {
_facebookUpdateQueue = dispatch_queue_create("com.facebookUpdateQueue", NULL);
}
}
我認為我的總體策略是合理的,但是我在嘗試如何在初始化期間真正獲取管理器的存檔版本時遇到了麻煩! 有什么建議嗎?
您的類需要實現<NSCoding>
及其兩個方法encodeWithCoder:
來歸檔所有屬性值,而initWithCoder:
來歸檔它們。 確保在實現中調用super。 通常,使用存檔類的類會知道有關存檔的信息,但是您可以通過使用initForReadingWithData:
創建NSKeyedUnarchiver
然后調用[self initWithCoder:...];
來在init
隱藏該知識[self initWithCoder:...];
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.