2012-09-20 96 views
1

好吧,我上週已經過了這一百萬次了,而我卻沒有得到它。 (是的,我讀過蘋果的文檔。)無法恢復歸檔數據

我歸檔我的對象,它似乎正在歸檔正確(我可以看到文件寫入文件系統,如果我檢查它,我可以看到我的數據)。但是,當我重新啓動我的應用程序時,我的數據未被恢復。我讀過的每個例子告訴我這是多麼容易,但我只是沒有得到它。唯一的一點是我的對象是一個單例,它用於在視圖控制器之間傳遞數據。

我真的很感謝一些賢者的建議。提前致謝。

這裏是我的頭:

#import <Foundation/Foundation.h> 

@interface SharedAppDataObject : NSObject <NSCoding> 
{ 
    NSMutableDictionary *apiKeyDictionary; 
    NSString *skuFieldText; 
    NSIndexPath *checkmarkIndex; 
} 

+ (SharedAppDataObject *)sharedStore; 

@property (nonatomic, copy) NSString *skuFieldText; 
@property (nonatomic, copy) NSIndexPath *checkmarkIndex; 
@property (nonatomic, copy) NSMutableDictionary *apiKeyDictionary; 

-(void)setValue:(NSString *)apiKey forKey:(NSString *)name; 

-(void)setSkuField:(NSString *)s; 
-(void)setCheckmarkIndex:(NSIndexPath *)p; 

-(NSMutableDictionary *)apiKeyDictionary; 
-(BOOL)saveChanges; 

@end 

這裏是我的實現:從應用程序委託

#import "SharedAppDataObject.h" 

@implementation SharedAppDataObject 

@synthesize skuFieldText; 
@synthesize checkmarkIndex; 
@synthesize apiKeyDictionary; 


    //create our shared singleton store 
+(SharedAppDataObject *)sharedStore { 

    static SharedAppDataObject *sharedStore = nil; 

    if (!sharedStore) { 
     sharedStore = [NSKeyedUnarchiver unarchiveObjectWithFile:[SharedAppDataObject archivePath]]; 

     if(!sharedStore) 
      sharedStore = [[super allocWithZone:NULL] init]; 
    } 
    return sharedStore; 
} 

-(id) init { 
    self = [super init]; 
    if (self) { 
    } 
    return self; 
} 

-(void)setValue:(id)apiKey forKey:(NSString *)name { 
     [apiKeyDictionary setObject:apiKey forKey:name]; 
} 

-(void)setSkuField:(NSString *)s { 
    skuFieldText = s; 
} 

-(NSMutableDictionary *)apiKeyDictionary { 
    return apiKeyDictionary; 
} 

-(void)setCheckmarkIndex:(NSIndexPath *)p { 
    checkmarkIndex = p; 
} 

-(void)encodeWithCoder:(NSCoder *)aCoder { 
    [aCoder encodeObject:skuFieldText forKey:@"skuFieldText"]; 
    [aCoder encodeObject:checkmarkIndex forKey:@"checkmarkIndex"]; 
    [aCoder encodeObject:apiKeyDictionary forKey:@"apiKeyDictionary"]; 
} 

-(id)initWithCoder:(NSCoder *)aDecoder { 
    self = [super init]; 
    if (self) { 
     [self setSkuFieldText:[aDecoder decodeObjectForKey:@"skuFieldText"]]; 
     [self setCheckmarkIndex:[aDecoder decodeObjectForKey:@"checkmarkIndex"]]; 
     [self setApiKeyDictionary:[aDecoder decodeObjectForKey:@"apiKeyDictionary"]]; 
    } 
    return self; 
} 

+(NSString *)archivePath { 
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirectory = [documentDirectories objectAtIndex:0]; 
    return [documentDirectory stringByAppendingPathComponent:@"bbyo.archive"]; 
} 

-(BOOL)saveChanges {  
    return [NSKeyedArchiver archiveRootObject:self toFile:[SharedAppDataObject archivePath]]; 
} 

@end 

保存方法:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    BOOL success = [[SharedAppDataObject sharedStore] saveChanges]; 

    if (success) { 
     NSLog(@"Saved all the data"); 
    } else { 
     NSLog(@"Didn't save any of the data"); 
    } 
} 

回答

0

初始化sharedStore = [NSKeyedUnarchiver unarchiveObjectWithFile:[SharedAppDataObject archivePath]]; in application:didFinishLaunchingWithOptions:。此方法用於初始化數據結構並恢復以前的應用程序狀態。

另外,從sharedStore取出static SharedAppDataObject *sharedStore = nil;。如果保存文件存在,[ShareAppDataObject sharedStore]將始終取消存檔不必要的文件。在初始化期間可以將其取消存檔一次。

這裏有一個帖子說可以回答你的問題:http://bit.ly/PJO8fM

+0

您鏈接到的例子是完美的。謝謝! – Selch

0

我不能給你答案,但一些想法,以圖這一點。採取以下線路:

sharedStore = [NSKeyedUnarchiver unarchiveObjectWithFile:[SharedAppDataObject archivePath]]; 

所以,如果sharedStore是零,有些事情是錯的 - 所以測試它。如果沒有,那麼記錄路徑,並使用NSFileManager方法來查看文件是否在那裏,它的大小等等。如果你發現文件存在並且有大小,但你不能解除它的存檔,那當然是個問題。在這種情況下,添加特殊調試代碼創建文件剛過:

-(BOOL)saveChanges {  
    BOO ret = [NSKeyedArchiver archiveRootObject:self toFile:[SharedAppDataObject archivePath]]; 
    id foo = [NSKeyedUnarchiver unarchiveObjectWithFile:[SharedAppDataObject archivePath]]; 
    // check if foo is not nil, if its the proper class, etc. 
} 

如果當您保存文件,你可以解除封存得很好,但不能在應用程序的重新啓動,那麼什麼是錯與文件。所有這些信息都應該指向解決方案。

另一個想法 - 當你編碼數據,記錄它,只是爲了確保它不是零 - 但即使如此unarchive應該工作。