2013-12-14 73 views
1

我正在为我的应用程序使用核心数据。它在模拟器上正常工作,但不会在实际设备上检索到细节。设备是iOS6.1.Device是我使用的代码:核心数据在设备上无法正常工作,但在模拟器上正常工作

- (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; 
} 

,但我没有得到关于设备的用户的详细信息,但得到的simulator.anyone面对这个一样吗?

+1

你可以张贴'的NSLog(@ “%@”,objUser)的输出;'。顺便说一句:请不要将getter方法加上'get'前缀。只需“ - (User *)activeUser' - 按照惯例:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html –

+0

你做了什么调试?至少记录每种方法返回的错误... – Wain

+0

@FlorianMielke感谢您的信息。我在objUser中获取的是:(实体:用户; ID:0x1facde20 ; data:) 2013-12-14 18:31:31.811 ForcePack 1.3 [3851:7607] (entity:User; id:0x1faccb70 ; data:) –

回答

0

就你而言,我建议ARC在执行提取请求后发布managedObjectContext

请确保在您的应用的某个位置(例如您的ApplicationDelegate)的某个位置的整个有效期内,您持有参考适当的managedObjectContext。没有它的managedObjectContext,一个NSManagedObject不能生活。核心数据项目模板显示了如何做到这一点。

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

+0

sure.will试一下 –

+0

非常感谢你们这个简单的东西偷了我的时间。我甚至尝试用新的核心数据替换核心数据。:) –

+0

欢迎您。在开始使用Core Data时,我也偶然发现了这一点。 –

相关问题