coreData-Fetching Managed Objects
https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/fetching.html
This article contains snippets for fetching managed objects.
To fetch managed objects, you minimally need a managed object context against which to execute the fetch, and an entity description to specify the entity you want. You create an instance of NSFetchRequest and specify its entity. You may optionally specify an array of sort orderings and/or a predicate.
How you get the managed object context depends on your application architecture—see Getting a Managed Object Context. Once you have the context, though, you can get the entity using NSEntityDescription’s convenience method, entityForName:inManagedObjectContext:.
Basic Fetch
To get all the managed objects of a given entity, create a fetch request and specify just the entity:
NSManagedObjectContext *context = <#Get the context#>; |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" |
inManagedObjectContext:context]; |
[fetchRequest setEntity:entity]; |
NSError *error; |
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; |
if (fetchedObjects == nil) {
|
// Handle the error. |
} |
Fetch with Sorting
To fetch managed objects in a particular order, in addition to the components in the basic fetch (described in Basic Fetch) you need to specify an array of sort orderings:
NSManagedObjectContext *context = <#Get the context#>; |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" |
inManagedObjectContext:context]; |
[fetchRequest setEntity:entity]; |
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"<#Sort key#>" |
ascending:YES]; |
NSArray *sortDescriptors = @[sortDescriptor]; |
[fetchRequest setSortDescriptors:sortDescriptors]; |
NSError *error; |
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; |
if (fetchedObjects == nil) {
|
// Handle the error. |
} |
Fetch with a Predicate
To fetch managed objects that meet given criteria, in addition to the components in the basic fetch (described in Basic Fetch) you need to specify a predicate:
NSManagedObjectContext *context = <#Get the context#>; |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" |
inManagedObjectContext:context]; |
[fetchRequest setEntity:entity]; |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"<#Predicate string#>", |
<#Predicate arguments#>]; |
[fetchRequest setPredicate:predicate]; |
NSError *error; |
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; |
if (fetchedObjects == nil) {
|
// Handle the error. |
} |
For more about predicates, see Predicate Programming Guide. For an alternative technique for creating the predicate that may be more efficient, see Fetch with a Predicate Template.
Fetch with a Predicate Template
To fetch managed objects that meet given criteria, in addition to the components in the basic fetch (described in Basic Fetch) you need to specify a predicate.NSPredicate’s predicateWithFormat: method is typically the easiest way to use a predicate (as shown in Fetch with a Predicate), but it’s not the most efficient way to create the predicate itself. The predicate format string has to be parsed, arguments substituted, and so on. For performance-critical code, particularly if a given predicate is used repeatedly, you should consider other ways to create the predicate. For a predicate that you might use frequently, the easiest first step is to create a predicate template. You might create an accessor method that creates the predicate template lazily on demand:
// Assume an instance variable: |
// NSPredicate *predicateTemplate; |
- (NSPredicate *)predicateTemplate {
|
if (predicateTemplate == nil) {
|
predicateTemplate = [NSPredicate predicateWithFormat:@"<#Key#> <#Operator#> <#$Variable#>"]; |
} |
return predicateTemplate; |
} |
When you need to use the template, you create a dictionary containing the substitution variables and generate the predicate using predicateWithSubstitutionVariables:.
NSManagedObjectContext *context = <#Get the context#>; |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" |
inManagedObjectContext:context]; |
[fetchRequest setEntity:entity]; |
NSDictionary *variables = @{ @"<#Variable#>" : <#Value#> };
|
NSPredicate *predicate = [[self predicateTemplate] |
predicateWithSubstitutionVariables:variables]; |
[fetchRequest setPredicate:predicate]; |
NSError *error; |
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; |
if (fetchedObjects == nil) {
|
// Handle the error. |
} |
For more about predicates, see Predicate Programming Guide.
Fetch with Sorting and a Predicate
To fetch managed objects that meet given criteria and in a particular order, in addition to the components in the basic fetch (described in Basic Fetch) you need to specify a predicate and an array of sort orderings.
NSManagedObjectContext *context = <#Get the context#>; |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" inManagedObjectContext:context]; |
[fetchRequest setEntity:entity]; |
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"<#Sort key#>" ascending:YES]; |
NSArray *sortDescriptors = @[sortDescriptor]; |
[fetchRequest setSortDescriptors:sortDescriptors]; |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"<#Predicate string#>", |
<#Predicate arguments#>]; |
[fetchRequest setPredicate:predicate]; |
NSError *error; |
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; |
if (fetchedObjects == nil) {
|
// Handle the error. |
} |
For more about predicates, see
coreData-Fetching Managed Objects的更多相关文章
- iOS10 CoreData新特性
原文地址:What's New in Core Data in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0 翻译者:肖品,原创文章转载请著名出处 ...
- iOS-数据持久化-CoreData
CoreData详解 介绍: 在Cocoa环境下,如果你想使用数据库(如sqlite),你可以使用sql语句的方式通过相关的工具类进行数据库的直接操作.当然你也可以通过别人封装之后的一些简单框架,使得 ...
- iOS CoreData学习资料 和 问题
这里是另一篇好文章 http://blog.csdn.net/kesalin/article/details/6739319 这里是另一篇 http://hxsdit.com/1622 (不一定能访问 ...
- iphone dev 入门实例4:CoreData入门
The iPhone Core Data Example Application The application developed in this chapter will take the for ...
- iOS CoreData (1)
下面开始学习一下CoreData. Core Data不是一个关系型数据库,也不是关系型数据库管理系统(RDBMS). Core Data 为数据变更管理.对象存储.对象读取恢复的功能提供了支持. 它 ...
- 使用CoreData [4]
使用CoreData [4] 此片文章主要是分析如何对CoreData进行封装. 在开始之前,我们需要弄明白3个非常关键的类,以下翻译凑合着看看. NSManagedObjectContext An ...
- iOS应用开发之CoreData[转]
我目前的理解,CoreData相当于一个综合的数据库管理库,它支持sqlite,二进制存储文件两种形式的数据存储.而CoreData提供了存储管理,包括查询.插入. 删除.更新.回滚.会话管理.锁管理 ...
- ios之coredata(一)
下面开始学习一下CoreData. Core Data不是一个关系型数据库,也不是关系型数据库管理系统(RDBMS).Core Data 为数据变更管理.对象存储.对象读取恢复的功能提供了支持. 它可 ...
- ios CoreData NSManagedObject 生命周期
用同样的检索条件从context检索出的对象是一个????所以 在主页的3个brand没法释放,在仅仅处理brand的时候???? 和 多个 context无关 我重写了NSManagedObject ...
随机推荐
- ACM-ICPC2018焦作网络赛 Participate in E-sports(大数开方)
Participate in E-sports 11.44% 1000ms 65536K Jessie and Justin want to participate in e-sports. E- ...
- FZU - 2214 Knapsack problem 01背包逆思维
Knapsack problem Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...
- PHP之递归函数
https://www.cnsecer.com/4146.html http://www.jb51.net/article/71424.htm //一列数字的规则如下:1,1,2,3,5,8,13,2 ...
- JS 识别生日、性别、年龄
<script> function IdCard(UUserCard,num){ if(num==1){ //获取出生日期 birth=UUserCard.substring(6, 10) ...
- python 之 函数 面向过程 三元表达式 函数递归
5.11 面向过程编程思想 核心是'过程'二字,过程即解决问题的步骤,即先干什么,再干什么........ 基于面向过程编写程序就好比在设计一条流水线,是一种机械式的思维方式. 总结优缺点: 优点:复 ...
- java基础第十篇之异常
1.1接口概念 类:具有相同属性和功能的事物集合 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于 ...
- 微信小程序请求openid错误码40163
通过wx.login({})方法获取到的code只能使用一次,如果需要在哎服务器端再次请求获取openid来进行校验,需要再次通过wx.login({})方法获取code 否则会报错误代码40163, ...
- [TCP/IP]TCP的三次握手和四次挥手
概述 总结一下TCP中3次握手过程,以及其原生的缺陷 引起的SYN Flood的介绍 1.TCP连接建立--三次握手 几个概念: seq:序号,占4个字节,范围[0,4284967296],由于TCP ...
- #1369 : 网络流一·Ford-Fulkerson算法 模板题
http://hihocoder.com/problemset/problem/1369?sid=1108721 别人都说先学网络流再学二分图,但是我先学了二分图的,感觉网络流好高端啊. 首先对于原图 ...
- (转)linux mount (挂载命令)详解
linux mount (挂载命令)详解 原文:http://tutu.spaces.eepw.com.cn/articles/article/item/70737 挂接命令(mount) 首先,介绍 ...