[英]RestKit - Data not persisting between app launches
In my RestKit application, I need to store an email and password in CoreData (I know about NSUserDefaults, but I have other user properties I need stored in CoreData as well). 在我的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];
This user object is always null at the application launch. 在应用程序启动时,此用户对象始终为null。
Then, when the user enters his/her information, I save it like so: 然后,当用户输入他/她的信息时,我将其保存为:
- (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"];
}
Afterwards, when I try to get the User data from the first function, it returns with all the information. 然后,当我尝试从第一个函数获取User数据时,它将返回所有信息。 However, when I relaunch the program, the data is null again.
但是,当我重新启动该程序时,数据再次为空。 Why is this data not persisting between application launches?
为什么这些数据在应用程序启动之间不存在?
EDIT: In case I screwed something up in my initialization, I've pasted it below: 编辑:如果我在初始化中搞砸了,我将其粘贴在下面:
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];
This is how to add persistent storage 这是添加永久存储的方法
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);
}
And whenever you are done with the changes, dont forget to save 每当您完成更改后,请不要忘记保存
if(![self.managedObjectContext saveToPersistentStore:&error]){
You never create a user anywhere in the code you posted. 您永远不会在发布的代码中的任何地方创建用户。 You first fetch all users - and none come back.
您首先要获取所有用户,但没有人回来。 Then you fetch them again - why do you expect that there is one now?
然后,您再次获取它们-您为什么期望现在有一个? Did you check if the user is
nil
the second time as well? 您是否也第二次检查用户是否
nil
?
What you need to to is to insert a new User object if there is none: 您需要插入一个新的User对象(如果没有):
User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User"
inManagedObjectContext:context];
// configure the user
[context save:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.