簡體   English   中英

RestKit-應用啟動之間的數據不持久

[英]RestKit - Data not persisting between app launches

在我的RestKit應用程序中,我需要在CoreData中存儲電子郵件和密碼(我知道NSUserDefaults,但是我還需要在CoreData中存儲其他用戶屬性)。

NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSError *error = nil;
NSEntityDescription *userEntity = [NSEntityDescription
                                   entityForName:@"User" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:userEntity];
NSArray *users = [context executeFetchRequest:request error:&error];
return (User *)[users firstObject];

在應用程序啟動時,此用戶對象始終為null。

然后,當用戶輸入他/她的信息時,我將其保存為:

- (void)cacheEmailAndPassword:(NSDictionary *)credentials {

NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;

NSError *error = nil;
NSEntityDescription *userEntity = [NSEntityDescription
                                   entityForName:@"User" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:userEntity];
NSArray *users = [context executeFetchRequest:request error:&error]; //should be a single object array for now
User *user = (User *)[users firstObject];

user.email = [credentials objectForKey:@"email"];
user.password = [credentials objectForKey:@"password"];
}

然后,當我嘗試從第一個函數獲取User數據時,它將返回所有信息。 但是,當我重新啟動該程序時,數據再次為空。 為什么這些數據在應用程序啟動之間不存在?

編輯:如果我在初始化中搞砸了,我將其粘貼在下面:

NSError *error = nil;
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"]];
// NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

// Initialize the Core Data stack
[managedObjectStore createPersistentStoreCoordinator];

// Configure the object manager
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:BASE_URL]];
objectManager.managedObjectStore = managedObjectStore;

NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
NSAssert(persistentStore, @"Failed to add persistent store: %@", error);

[managedObjectStore createManagedObjectContexts];

// Set the default store shared instance
[RKManagedObjectStore setDefaultStore:managedObjectStore];



[RKObjectManager setSharedManager:objectManager];

這是添加永久存儲的方法

NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"storage" ofType:@"momd"]];
// NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
    RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"MyStore.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (! persistentStore) {
    RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}

每當您完成更改后,請不要忘記保存

if(![self.managedObjectContext saveToPersistentStore:&error]){

您永遠不會在發布的代碼中的任何地方創建用戶。 您首先要獲取所有用戶,但沒有人回來。 然后,您再次獲取它們-您為什么期望現在有一個? 您是否也第二次檢查用戶是否nil

您需要插入一個新的User對象(如果沒有):

User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" 
             inManagedObjectContext:context];

// configure the user
[context save:nil]; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM