- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
当为 NSManagedObjectContext
使用 NSPrivateQueueConcurrencyType
和 NSMainQueueConcurrencyType
类型时,在同一上下文中进行嵌套的 performBlock 调用是否安全?
[backgroundContext performBlock:^{
NSFetchRequest *myRequest = ...;
__block NSArray *result= nil;
[backgroundContext performBlockAndWait:^{
results = [backgroundContext executeFetchRequest:myRequest error:NULL];
}];
}];
这可能看起来很愚蠢,但我有一个现有的代码库,其中包含许多封装 executeFetchRequest
调用的辅助方法。我不想假设调用者是否已经使用过 performBlock。例如:
-(void)updateObjects:(BOOL)synchronous
{
if (YES == synchronous)
[self fetchHelper];
else
{
[backgroundContext performBlock:^{
[self fetchHelper];
}];
}
}
-(NSArray*)fetchHelper
{
[self.backgroundContext performBlockAndWait:^{
//Fetch the objects...
[self.backgroundContext executeFetchRequest: (...)];
}];
}
我试过了,效果不错。但是我学会了(通过艰难的方式)对核心数据和多线程要非常小心。
最佳答案
是的,performBlockAndWait 是可重入的。直接来自 Apple 的发行说明...
Core Data formalizes the concurrency model for the NSManagedObjectContext class with new options. When you create a context, you can specify the concurrency pattern to use with it: thread confinement, a private dispatch queue, or the main dispatch queue. The NSConfinementConcurrencyType option provides the same behavior that was present on versions of iOS prior to 5.0 and is the default. When sending messages to a context created with a queue association, you must use the performBlock: or performBlockAndWait: method if your code is not already executing on that queue (for the main queue type) or within the scope of a performBlock... invocation (for the private queue type). Within the blocks passed to those methods, you can use the methods of NSManagedObjectContext freely. The performBlockAndWait: method supports API reentrancy. The performBlock: method includes an autorelease pool and calls the processPendingChanges method upon completion.
关于ios - 嵌套的 performBlock : on NSManagedObjectContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10445246/
我尝试为我的 UITableViewController 使用 NSFetchedResultsController(FRC),因为我喜欢 FRC 附带的功能,而不是尝试手动管理我的 UITableV
我的应用程序中目前有 2 个上下文。我的应用程序使用多个选项卡,因此一个选项卡可能正在显示数据,而另一个选项卡可能处于数据输入模式。 我使用一个主要上下文来读取大部分数据以进行显示。当我插入数据时,我
我在 appDelegate 中创建了一个主要的 NSManagedObjectContext。 现在,我正在使用另一个 NSManagedObjectContext 来编辑/添加新对象,而不会影响主
假设我们有一个应用程序需要显示地点列表并且在 3 个线程上运行: 主线程 主线程后台同步(同步有服务器的地方) 地理编码线程(对位置进行地理编码背景) 在所有 3 个线程中,我都有专用的 NSMana
所以我需要使用多线程来使用 cora-data 托管对象上下文。我发现下面的实现几乎是我所需要的。我有 2 个问题: 在哪里释放我分配的 MOC?我不想遍历应用程序中的每个函数线程并调用函数来释放 M
我正在开发一个使用 coredata 的 ipad 应用程序。它下载网络数据库上的信息,并将其记录在 coredata 中。该应用程序基于分割 View 。我的问题是在后台下载和记录数据。 这是我的做
有没有办法在主线程之外的后台保存我的 NSManagedObjectContext ?保存会减慢应用程序的执行速度,因为它通常需要大约 2 秒。 最佳答案 是的,有。 Apple recommends
在 iPhone 的核心数据中,我在尝试将数据保存到 NSManagedObjectContext 时遇到了各种错误。 我相信我的问题都与我使用在多个线程中使用的 NSManagedObjectCon
我有两个实体,每个实体都显示在自己的 UITableView 部分上。 我启用了编辑功能,允许用户通过向右滑动来删除行。这对于第一个实体来说效果很好,但是当我尝试删除第二个实体中的对象时,我收到此错误
我有以下设置: NSManagedObjectContext *parent = [[NSManagedObjectContext alloc]
在调试时,如果我有一个 NSManagedObjectContext,有没有办法查看里面有什么对象。 基本上我有一个保存错误,因为保存了一个不符合 NSCoding 的 CGColor。但我不知道这个
我正在尝试制作一个用户可以编辑 managedObject 属性的应用程序在 View 中,然后选择 Done保留更改,或 Cancel撤消更改。 为了实现此行为,我计划执行以下操作 - 当 View
最近我遇到了一个无法解释的问题。所以我有一个 DocumentContext 类,其中包含一个 Document (NSManagedObject)。这些类的代码是 extension Documen
我想在插入新数据之前清除 CoreData 实体中的所有数据。到目前为止,我尝试过删除所有内容,并且不允许我插入更新的数据,即使对这两个操作使用相同的上下文也是如此。有更好的方法吗?这是我一直在实现的
在我的应用中,我将 Core Data 堆栈从 AppDelegate 移至其自己的名为 LiftEventDataManager 的类中。在一些类中,我获得了对该堆栈的引用,当然,每次我执行操作时都
我正在使用以下代码创建一个对象: +(Checkin *) newCheckinWithId:(NSString*) checkinID forVenueId:(NSString *)venueId
我是 Core Data 的新手。 我的数据模型有一个 User 实体,它有一个属性 fullName,它是从服务器获取的。我使用 NSFetchedResultsController 在 Table
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingSt
我使用 Core Data - 我已经注册并正在监听 NSManagedObjectContextDidSaveNotification:我有一个数据集合(来自 JSON),我想要保存它,在保存所有对
我的核心数据堆栈像往常一样在 AppDelegate 中设置。我是一个优秀的 OO 公民,并认识到可以通过 [[UIApplication sharedApplication] delegate] m
我是一名优秀的程序员,十分优秀!