1

我正在为我的应用程序使用核心数据。它在模拟器上运行良好,但不能在真实设备上检索详细信息。设备是 iOS6.1。这是我正在使用的代码:

- (NSManagedObjectContext *) getCurrentMangedContext
{
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"ForceData" withExtension:@"momd"];
    NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];  

    NSURL *storeURL =  [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]URLByAppendingPathComponent:@"ForceData.sqlite"];

    NSError *error = nil;
    NSPersistentStoreCoordinator *persistantStroreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
    if (![persistantStroreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {

    }
    NSManagedObjectContext *managedContext = [[NSManagedObjectContext alloc] init] ;
    [managedContext setPersistentStoreCoordinator:persistantStroreCoordinator];
    modelURL = nil;
    return  managedContext;
}

这就是我保存登录详细信息的方式,它没有给出任何错误。

 NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
  [request setEntity:entity];
  NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
    if (emailString != nil)
    {
       [newManagedObject setValue:emailString forKey:@"email"]; 
    }
    if (genderString != nil)
    {
        [newManagedObject setValue:genderString forKey:@"gender"]; 
    }
    if (fNameString != nil)
    {
        [newManagedObject setValue:fNameString forKey:@"firstName"]; 
    }
    if (lNameString != nil)
    {
        [newManagedObject setValue:lNameString forKey:@"lastName"]; 
    }
   if (userIDString != nil)
    {
        [newManagedObject setValue:userIDString forKey:@"userID"]; 
    }
  NSError *error = nil;
    if (![context save:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    // Insert User details to the user DB <--------------------------

这就是我检索的方式:

- (User *) getActiveUser 
{
    NSManagedObjectContext *context = [self getCurrentMangedContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
    [request setEntity:entity];

    NSError *errorFetch = nil;
    NSArray *array = [context executeFetchRequest:request error:&errorFetch];
    User *objUser = (User *) [array lastObject];
    NSLog(@"%@",objUser);

    return objUser;
}

但我没有在设备上获取用户详细信息,而是在模拟器上获取。有人遇到过同样的情况吗?

4

1 回答 1

0

在您的情况下,我建议 ARCmanagedObjectContext在执行 fetch 请求后释放您。

确保您在应用程序中某个位置(例如您的 )的整个生命周期中都持有strong对适当的引用。没有managedObjectContext 就无法生存。Core Data 项目模板展示了如何做到这一点。managedObjectContextmanagedObjectApplicationDelegateNSManagedObject

有关 ARC 和强引用的更多信息:https ://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

于 2013-12-14T13:18:45.373 回答