无法从Core Data获取不同的属性值

时间:2013-06-10 08:01:29

标签: ios core-data

下面有两个功能,但只有一个有结果。在“noResult”中,我试图获取一个不同属性值的列表,但我得到的只是{}。

-(void)noResult {
  NSManagedObjectContext *moc= [xmppMessageArchivingStorage mainThreadManagedObjectContext];
  NSEntityDescription *entity= [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject" inManagedObjectContext:moc];
  NSFetchRequest *request= [[NSFetchRequest alloc] init];
  [request setEntity:entity];
  [request setResultType:NSDictionaryResultType];
  [request setReturnsDistinctResults:YES];
  [request setPropertiesToFetch:[NSArray arrayWithObject:@"contact"]];

  NSArray *messages= [moc executeFetchRequest:request error:nil];
  NSLog(@"%@", messages); 
}

-(void)gotResult {
   NSManagedObjectContext *moc= [xmppMessageArchivingStorage mainThreadManagedObjectContext];
   NSEntityDescription *entity= [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject" inManagedObjectContext:moc];
   NSFetchRequest *request= [[NSFetchRequest alloc] init];
   [request setEntity:entity];
   NSArray *messages= [moc executeFetchRequest:request error:nil];
}

以下是“gotResult” “(entity:XMPPMessageArchiving_Message_CoreDataObject; id:0x1c5d7fc0; data:{\ n bareJid = nil; \ n bareJidStr = nil; \ n body = \”Bean bags \“; \ n composing = 0; \ n contact = 1000743469142; \ n from = 7375011; \ n message = nil; \ n messageStr = nil; \ n outgoing = 0; \ n streamBareJidStr = nil; \ n thread = nil; \ n timestamp = \“2013-06-05 10:56:38 0000 \ “; \ N})”

1 个答案:

答案 0 :(得分:2)

我能做出不同行为的唯一原因 想到的是你在调用之前没有保存托管对象上下文 使用NSDictionaryResultType获取请求。

[request setResultType:NSDictionaryResultType];

意味着

[request setIncludesPendingChanges:NO];

这意味着从fetch返回的数组反映了持久存储中的当前状态,并没有考虑上下文中的任何挂起的更改,插入或删除。