博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MagicalRecord使用中的注意事项
阅读量:7126 次
发布时间:2019-06-28

本文共 2864 字,大约阅读时间需要 9 分钟。

MagicalRecord使用中的注意事项

 

 

将bundle中的CoreData相关文件复制到沙盒目录中并使用

复制这三个文件

然后用单例初始化

////  GlobalCoreData.h//  YXMWeather////  Created by XianMingYou on 15/2/20.//  Copyright (c) 2015年 XianMingYou. All rights reserved.//#import 
#import "CityCode.h"@interface GlobalCoreData : NSObject+ (void)globalInit;@end
////  GlobalCoreData.m//  YXMWeather////  Created by XianMingYou on 15/2/20.//  Copyright (c) 2015年 XianMingYou. All rights reserved.//#import "GlobalCoreData.h"@implementation GlobalCoreData+ (void)globalInit {    GCDGroup *group = [GCDGroup new];    if ([@"/Documents/CityCode.sqlite" exist] == NO) {        [[GCDQueue globalQueue] execute:^{            NSString *srcPath = [@"CityCode.sqlite" bundleFile];            NSString *dstPath = [@"/Documents/CityCode.sqlite" path];            [[NSFileManager defaultManager] copyItemAtPath:srcPath                                                    toPath:dstPath                                                     error:nil];        } inGroup:group];    }    if ([@"/Documents/CityCode.sqlite-shm" exist] == NO) {        [[GCDQueue globalQueue] execute:^{            NSString *srcPath = [@"CityCode.sqlite-shm" bundleFile];            NSString *dstPath = [@"/Documents/CityCode.sqlite-shm" path];            [[NSFileManager defaultManager] copyItemAtPath:srcPath                                                    toPath:dstPath                                                     error:nil];        } inGroup:group];    }    if ([@"/Documents/CityCode.sqlite-wal" exist] == NO) {        [[GCDQueue globalQueue] execute:^{            NSString *srcPath = [@"CityCode.sqlite-wal" bundleFile];            NSString *dstPath = [@"/Documents/CityCode.sqlite-wal" path];            [[NSFileManager defaultManager] copyItemAtPath:srcPath                                                    toPath:dstPath                                                     error:nil];        } inGroup:group];    }    [[GCDQueue globalQueue] notify:^{        [MagicalRecord setupCoreDataStackWithStoreAtURL:[NSURL fileURLWithPath:[@"/Documents/CityCode.sqlite" path]]];    } inGroup:group];}@end

此处要记得设置初始化路径.

        [MagicalRecord setupCoreDataStackWithStoreAtURL:[NSURL fileURLWithPath:[@"/Documents/CityCode.sqlite" path]]];

 

关于NSManagedObjectContext的一些使用细节

NSManagedObjectContext保存了操作数据库句柄的一些上下文,在MagicalRecord中,我们有时候会在子线程中进行查询操作,而到主线程中进行显示操作.问题来了,在实际使用过程中,操作句柄对象NSManagedObjectContext会在跨线程的过程中丢失掉,也就是说数据变成空了.

这个时候,我们需要将操作句柄NSManagedObjectContext定义成属性,然后用

[NSManagedObjectContext MR_context]

来获取出操作句柄并作为属性存储起来(防止丢失)

然后,你就可以做你自己想做的炒作了;)

比如以下操作:

    [GCDQueue executeInGlobalQueue:^{

        NSPredicate *searchInfo = [NSPredicate predicateWithFormat:@"%@ IN [cd] cityName", textField.text];

        self.magicArray = [CityCode MR_findAllWithPredicate:searchInfo

                                                  inContext:self.context];

        [GCDQueue executeInMainQueue:^{

            [self.tableView reloadData];

        }];

    }];

 

 

 

 

转载地址:http://rphel.baihongyu.com/

你可能感兴趣的文章
二叉树链接右指针II
查看>>
centos7+docker综合实验
查看>>
基本概念学习(7004)---三层交换机
查看>>
Awstats-日志分析
查看>>
用ImageBuilder定制自己的openwrt路由器
查看>>
shell脚本检测mysql是否启动的方法
查看>>
机器学习中的End-to-End到底是怎么回事?
查看>>
用qt creator做我的c编码工具
查看>>
文本内容不管有多少始终能垂直居中:
查看>>
Linux下的Cacti网络管理系统--安装常见问题(二)
查看>>
Windows Server 2008 R2换SID要注意
查看>>
【世界知名量子科学家加盟阿里】施尧耘出任阿里云量子技术首席科学家
查看>>
Linux W命令详解
查看>>
bind9报 bad owner name (check-names)错误的解决方法
查看>>
1056. 组合数的和
查看>>
centos 较准时间
查看>>
Linux小白的大师之路
查看>>
小米9售价揭晓!可能是最后一次了
查看>>
py2exe的安装
查看>>
vrrp
查看>>