简单的核心数据获取
问题描述:
即时通讯使用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);
这样的IM肯定有我的实体属性“productPoints” [检查在sqlite管理器中)
如何查看数据?
在我的日志我看到
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>)"
所以我想即时看到我的实体的两个对象,但怎么看属性,
的感谢!
答
您猜测的对象在results
阵列中。要查看属性所有你需要做的就是访问它们,使用的东西沿着线:
productPoints* firstProduct = [results objectAtIndex:0];
NSLog("Some property value: %@", firstProduct.someProperty);
还要注意的是标准的核心数据API的是一个应该框架绝对荒谬简化任务存储和检索数据。我强烈建议您尝试使用讨论的NSManagedObjectContext+EasyFetch
类别here和github here。
然后你的代码可以被改写为:
PFIWIAppDelegate* delegate = (PFIWIAppDelegate*)[[UIApplication sharedApplication] delegate];
NSArray* results = [[delegate managedObjectContext] fetchObjectsForEntityName: @"productPoints"];
NSLog(@"tu fetch master db ::%@",results);
伟大aroth!谢谢!,为答案和> NSManagedObjectContext + EasyFetch – MaKo 2011-12-14 04:25:45