我有一個方法執行一個NSFetchRequest來獲得一個託管對象(特別是XMPPUserCoreDataStorageObjects)的數組。這些對象在performUserFetch返回數組之前正確地錯誤,並且我可以打印所有的displayNames,但是一旦將數組返回給printUserInfo,對象就會進入故障狀態,除了Core Data不會帶來問題外他們回來!NSArray核心數據錯誤
- (NSArray*)performUserFetch
{
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:[xmppRosterStorage persistentStoreCoordinator]];
[context setUndoManager:nil];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject" inManagedObjectContext:context];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError *err;
NSArray *result = [context executeFetchRequest:fetchRequest error:&err];
return result;
}
- (void)printUserInfo
{
NSArray *result = [self performUserFetch];
for(XMPPUserCoreDataStorageObject *user in result)
{
NSString *dn = user.displayName;
NSLog(@"Display name = %@", dn);
}
NSLog(@"%@",result);
}
我打電話printUserInfo每5秒,並在performUserFetch如果結果數組精細,但一切都已經走了鍋在printUserInfo,以及陣列中的所有元素已經從內存中清除。這將是很好,但是當我打電話user.displayName該故障並沒有解決,所以DN具有null值,並且用戶的描述是
"<XMPPUserCoreDataStorageObject: 0x10219fd60> (entity: XMPPUserCoreDataStorageObject; id: 0x1021a3390 <x-coredata://324B9E93-BAD1-42B4-B7DB-2A62CA69BA13/XMPPUserCoreDataStorageObject/p127> ; data: <fault>)"
誰能幫助?
(信息:10.7 SDK,printUserInfo被稱爲每隔5秒,ARC下運行)
只是好奇,我遇到了完全相同的問題。你有沒有解決這個問題? – 2014-02-21 21:35:50
您應該選擇Mundi的答案作爲答案 – kdbdallas 2014-09-06 22:10:57