RestKit - 应用程序启动之间不存在数据

时间:2014-07-17 20:49:49

标签: ios core-data restkit

在我的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"];
}

之后,当我尝试从第一个函数获取用户数据时,它返回所有信息。但是,当我重新启动程序时,数据再次为空。为什么这些数据在应用程序启动之间不会持久存在?

编辑:如果我在初始化时搞砸了一些东西,我已将其粘贴在下面:

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];

2 个答案:

答案 0 :(得分:1)

这是添加持久存储的方法

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]){

答案 1 :(得分:0)

您永远不会在您发布的代码中的任何位置创建用户。您首先获取所有用户 - 但没有人回来。然后你再次获取它们 - 你为什么期望现在有一个?您是否第二次检查用户是否为nil

如果没有,请插入新的User对象:

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

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