核心数据简单的Fetch

时间:2011-12-14 03:57:29

标签: ios core-data fetch

我使用coredata检查实体的内容,但仍记得如何去做,

PFIWIAppDelegate* delegate = (PFIWIAppDelegate*)[[UIApplication sharedApplication] delegate];


NSEntityDescription *entity = [NSEntityDescription entityForName:@"productPoints" inManagedObjectContext:[delegate managedObjectContext]]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

[request setEntity:entity];

NSLog(@" la resposta por deux:: %@", request);


NSError *error = nil;
NSArray *results = [[delegate managedObjectContext] executeFetchRequest:request error:&error];

NSLog(@"tu fetch master db ::%@",results);

所以我确定我的实体“productPoints”中有属性[在sqlite Manager中检查]

如何查看数据?

在我的日志中,我看到了

la resposta por deux:: <NSFetchRequest: 0x6cd1780> (entity: productPoints; predicate: ((null)); sortDescriptors: ((null)); type: NSManagedObjectResultType; )
2011-12-14 14:50:44.266 PFIWIN0196[7524:fb03] tu fetch master db ::(
"<productPoints: 0x6cd38c0> (entity: productPoints; id: 0x6cd2ce0 <x-coredata://888E340F-6CBF-4EED-B9D9-9C3FB06244F3/productPoints/p6> ; data: <fault>)",
"<productPoints: 0x6cd3b70> (entity: productPoints; id: 0x6cd2cf0 <x-coredata://888E340F-6CBF-4EED-B9D9-9C3FB06244F3/productPoints/p7> ; data: <fault>)"

所以我想我看到我的实体的2个对象,但是如何看到属性,

谢谢!

1 个答案:

答案 0 :(得分:3)

您猜测,您的对象位于results数组中。要查看属性,您只需使用以下内容访问它们:

productPoints* firstProduct = [results objectAtIndex:0];
NSLog("Some property value:  %@", firstProduct.someProperty);

另请注意,标准Core Data API对于应该简化存储和检索数据任务的框架来说是绝对荒谬的。我强烈建议您尝试使用NSManagedObjectContext+EasyFetch类别here和github here

然后您的代码可以重写为:

PFIWIAppDelegate* delegate = (PFIWIAppDelegate*)[[UIApplication sharedApplication] delegate];
NSArray* results = [[delegate managedObjectContext] fetchObjectsForEntityName: @"productPoints"];
NSLog(@"tu fetch master db ::%@",results);