ios - 核心数据简单获取

标签 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 管理器中检查]

如何查看数据?

在我的日志中我看到

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 个对象,但是如何查看属性,

谢谢!

最佳答案

正如您推测的那样,您的对象位于 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);

关于ios - 核心数据简单获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499320/

相关文章:

ios - 缩放时调整 UIScrollView 的内容

ios - 如何避免在 NSNumberFormatter 中四舍五入

ios - 在touchesBegan 和touchesEnded 之间是否有调用的方法?

ios - 核心数据关系不持久

ios - 核心数据有必要迁移吗?

ios - 初始化 sprite 对象时没有 'hero' 的已知类方法?

swift - 在基于文档的应用程序中为首选项 Controller 获取 managedObjectContext

javascript - 获取请求在移动设备上不起作用

git - 如何从git存储库中获取旧代码

javascript - 无法理解为什么它给我 : Uncaught (in promise) TypeError: Cannot read property 'blabla' of undefined