我正在爲我的應用程序使用核心數據。它在模擬器上正常工作,但不會在實際設備上檢索到細節。設備是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面對這個一樣嗎?
你可以張貼'的NSLog(@ 「%@」,objUser)的輸出;'。順便說一句:請不要將getter方法加上'get'前綴。只需「 - (User *)activeUser' - 按照慣例:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html –
你做了什麼調試?至少記錄每種方法返回的錯誤... – Wain
@FlorianMielke感謝您的信息。我在objUser中獲取的是:(實體:用戶; ID:0x1facde20 ; data:) 2013-12-14 18:31:31.811 ForcePack 1.3 [3851:7607] (entity:User; id:0x1faccb70 ; data:) –