CoreData
2017-03-14 17:27:00 4 举报
AI智能生成
IOS CoreData 基本操作
作者其他创作
大纲/内容
NSPersitentStoreCoordinator
存储的位置信息
NSManagerObjectModel
这个Model 类似一个 表结构
通过我们创建的 表名称momd 来初始化
增加
<p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures"> 1. WXCoreDataClubModel</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> * insertModel = (</span><span style="font-variant-ligatures: no-common-ligatures">WXCoreDataClubModel</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *)[</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSEntityDescription</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">insertNewObjectForEntityForName</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #78492a">tableName</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">inManagedObjectContext</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span><span style="font-variant-ligatures: no-common-ligatures">managedObjectContext</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p>
赋值 给 insertModel
context 执行 <span style="color: rgb(62, 30, 129); font-family: Menlo; font-size: 14px;">save</span>
查询
<p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures">1. 获取抽象表结构 NSEntityDescription</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *entity = [</span><span style="font-variant-ligatures: no-common-ligatures">NSEntityDescription</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">entityForName</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #78492a">tableName</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">inManagedObjectContext</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">managedObjectContext</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p>
<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures;">2. 获取Request NSFetchRequest</span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> *request = [[</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures;">NSFetchRequest</span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> </span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">alloc</span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">] </span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">init</span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">];</span><br><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures"> [request </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">setEntity</span><span style="font-variant-ligatures: no-common-ligatures">:entity];</span></p><br><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSPredicate</span><span style="font-variant-ligatures: no-common-ligatures"> *predicate = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSPredicate</span></p><br><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">predicateWithFormat</span><span style="font-variant-ligatures: no-common-ligatures">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"im_club_id like[cd] %@"</span><span style="font-variant-ligatures: no-common-ligatures">,im_club_id];</span></p><br><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures"> [request </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">setPredicate</span><span style="font-variant-ligatures: no-common-ligatures">:predicate];</span></p>
<p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">3. 获取结果 NSArray</span><span style="font-variant-ligatures: no-common-ligatures"> *result = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">self</span><span style="font-variant-ligatures: no-common-ligatures">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">managedObjectContext</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">executeFetchRequest</span><span style="font-variant-ligatures: no-common-ligatures">:request </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">error</span><span style="font-variant-ligatures: no-common-ligatures">:&error];</span></p>
删除
1.通过查询 获取 result< <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 14px;">NSManagedObject*>的数组</span>
2. 执行<span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">[</span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">self</span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">.</span><span style="color: rgb(79, 129, 135); font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures;">managedObjectContext</span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> </span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">deleteObject</span><span style="font-family: Menlo; font-size: 14px; font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);">:obj];</span>
<p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">3 [</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span><span style="font-variant-ligatures: no-common-ligatures">managedObjectContext</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">save</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p>
0 条评论
下一页