- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我正在使用 Core Data IOS 为 Objective-C 使用神奇的记录库。该库有许多 NSManageObjectContext 启动。我们应该使用什么来保持应用性能和良好的用户体验?
有很多
+ [NSManagedObjectContext MR_newContext]: Sets the default context as it's parent context. Has a concurrency type of NSPrivateQueueConcurrencyType.
+ [NSManagedObjectContext MR_newMainQueueContext]: Has a concurrency type of NSMainQueueConcurrencyType.
+ [NSManagedObjectContext MR_newPrivateQueueContext]: Has a concurrency type of NSPrivateQueueConcurrencyType.
+ [NSManagedObjectContext MR_newContextWithParent:…]: Allows you to specify the parent context that will be set. Has a concurrency type of NSPrivateQueueConcurrencyType.
+ [NSManagedObjectContext MR_newContextWithStoreCoordinator:…]: Allows you to specify the persistent store coordinator for the new context. Has a concurrency type of NSPrivateQueueConcurrencyType.
什么上下文启动是好的?
例如这个函数处理JSON响应并在成功接收到响应时将记录保存到数据库
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_context];
[Stamp MR_truncateAllInContext:localContext];
[responseJSON[@"root"] enumerateObjectsUsingBlock:^(id attributes, NSUInteger idx, BOOL *stop) {
Stamp *stamp = [Stamp MR_createEntityInContext:localContext];
[stamp setOrderingValue:idx];
[stamp updateWithApiRepresentation:attributes];
}];
[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
if (completionBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(!error, error);
});
}
}];
这个函数执行获取请求
+ (NSArray *)yearsDropDownValues
{
NSManagedObjectContext *moc = [NSManagedObjectContext MR_rootSavingContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [Stamp entityInManagedObjectContext:moc];
request.entity = entity;
request.propertiesToFetch = @[StampAttributes.year];
request.returnsDistinctResults = YES;
request.resultType = NSDictionaryResultType;
request.sortDescriptors = @[[[NSSortDescriptor alloc] initWithKey:StampAttributes.year ascending:NO]];
NSArray *years = [moc executeFetchRequest:request error:nil];
NSMutableArray *res = [NSMutableArray array];
for (NSDictionary *year in years) {
[res addObject:@{@"code": [NSString stringWithFormat:@"%@ Collections", year[@"year"]], @"value": year[@"year"] }];
}
return res;
}
非常感谢任何帮助。谢谢
最佳答案
在我开始之前,我认为还有两个上下文你应该知道和理解,当你使用 MagicalRecord 的默认方式设置你的 CoreData 堆栈时,它们是自动创建的:
MR_rootSavingContext
,它是一个直接连接到协调器的私有(private)队列上下文,通常它是您的根上下文。MR_defaultContext
,它是一个主队列上下文,它有 MR_rootSavingContext
作为它的父级,通常它是你的 UI 上下文,用它来获取和显示你的数据屏幕。现在我将这五个上下文一一解释:
MR_newContext
,一个新的私有(private)队列上下文,它有 MR_defaultContext
作为它的父上下文。它等同于调用 [NSManagedObjectContext MR_newContextWithParent:[NSManagedObjectContext ME_defaultContext]]
。这种类型的上下文适用于繁重的批处理操作,例如插入、更新、删除数百个对象。由于所有操作都在后台线程上运行,因此不会阻塞 UI。然而缺点是,它带来了额外的复杂性,尤其是当您有多个这样的上下文时,在保存这些上下文时可能会发生冲突。MR_newMainQueueContext
,一个没有父上下文的新主队列上下文。它是 MR_rootSavingContext
的兄弟,因为它们连接到相同的 NSPersistentStoreCoordinator
。您在此类上下文中执行的所有操作都会阻塞 UI,因此请勿在此上下文中执行任何繁重的工作。MR_newPrivateQueueContext
,类似于MR_newContext
,但没有父上下文。它是 MR_rootSavingContext
的兄弟。MR_newContextWithParent
,一种创建私有(private)队列上下文以及指定父上下文的简便方法。MR_newContextWithStoreCoordinator
,一个新的私有(private)队列上下文,它使用指定的 NSPersistentStoreCoordinator
。在我看来,只有知道如何正确使用具有不同协调器的上下文,才不要使用它。总之,这些上下文没有好坏之分,需要根据自己的需求选择合适的。
关于ios - Magical Record IOS objective C. 我们应该创建什么样的上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38690585/
谷歌搜索后,我发现可以使用魔数(Magic Number)来识别文件的内容类型。 在我的程序中,我想在服务器端验证文件内容类型。 我的客户端代码: function readF
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 6年前关闭。 Improve this questi
我在 Sonar 上收到此警告,认为这是违规行为。我需要适当的解决方案来从声纳中删除此警告。 我的代码是这样的: void method(){ try { int x; x=5; } ca
Cardano CLI 中有很多教程使用 --testnet-magic 3或 --testnet-magic 4在读取地址中的值、类型转换原生代币等过程中。但是他们都没有详细解释魔数(Magic N
我正在尝试编译“su” super 用户实用程序的修改版本,所以我在 git 上访问了 android5.0 源代码,并获得了“su.c”文件。 我的实际设备/硬件是乐视 x500 手机运行: And
我正在尝试编辑一些使用 python-magic 的代码,但出现导入错误:没有名为 magic 的模块。在我浏览互联网并找到有关使用我所做的 pip 安装 python-magic 的建议之前。我使用
您好,我正在尝试运行 python manage.py runserver 命令,但收到此错误消息 File "C:\Users\adrie\Anaconda3\lib\sre_compile.py"
我有这样一个函数: float_as_thousands_str_with_precision(value, precision) 如果我这样使用它: float_as_thousands_str_w
我有一个可以改变颜色的渐变,我希望其中的文本始终可见。 如果有任何开箱即用的资源,我宁愿动态地进行;我想要一个可以取消颜色的“魔笔”。 有什么实验吗? 最佳答案 Joel 对于如何对齐渐变画笔给出了很
Eclipse 生成的主细节流程在扩展 ListFragment 的类中有一些回调魔法 - 我说魔法是因为当你是 Android 和 Java 菜鸟时,它看起来就像是同时: ) 鉴于下面的代码,有人可
嗯,不完全是这样,但这里有一个例子。谁能解释一下B和C的区别?使用魔术函数动态设置值而不是简单地在属性定义中设置值如何更快? 这是一些代码: [root@vm-202-167-238-17 ~]# c
特地查看了下手册,关于php magic quotes,常见的几个设置如下,magic_quotes_gpc,magic_quotes_sybase,magic_quote_runtim
考虑这个类的安排 - 特别是魔术函数 __invoke: class Barman { public function __construct() { // .. .cons
是否可以在 Databricks 中导出“魔术 SQL”命令单元的输出? 我喜欢这样一个事实,即不必转义 SQL 命令,并且可以轻松地对其进行格式化。但是,我似乎无法在其他单元格中使用输出。我想要做的
我有来自 UploadedFile 按钮的文件,我想使用魔数(Magic Number)打印扩展文件, 我的代码: UploadedFile file = (UploadedFile)valueCha
我正在从头开始设计二进制文件格式,我想在开头包含一些魔术字节,以便可以轻松识别。我该如何选择哪些字节?我不知道任何魔数(Magic Number)的中央注册表,所以这只是选择一些尚未由附近 UNIX
问题:在 SQL 脚本或存储过程中避免魔数(Magic Number)或硬编码值的其他策略是什么? 考虑一个存储过程,它的工作是根据记录的StatusID 检查/更新记录的值。或其他一些 FK 查找表
我正在尝试使用Javascript的CryptoJS加密消息,如下所示: const encrypted_payload = CryptoJS.AES.encrypt("Hello, world!",
我使用“非常神奇”的正则表达式搜索(即/\v 或 %s/\v),但我希望我可以设置一些选项,这样我就不必再在任何地方包含\v 了。有办法做到这一点吗? 最佳答案 不是直接使用,但是您始终可以使用映射:
在哪里可以找到 Scala 的“神奇”函数列表,例如 apply、unapply、update、+= 等? 我所说的魔法函数是指编译器的某些语法糖使用的函数,例如 o.update(x,y) o(x
我是一名优秀的程序员,十分优秀!