相关疑难解决方法(0)

如何通过父MOC将更改从一个子托管对象上下文合并到另一个子托管对象上下文?

您好(实际问题在底部).

在iOS 5中,在CoreData中引入了父子管理对象上下文.

我有一个标准的NSFetchedResultsController和UITableVeiwController一起工作,从商店中获取主列表.获取的结果控制器的托管对象上下文是具有父上下文的子项:

// AppDelegate.m

- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }

    __managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

    // primary managed object context has NSPrivateQueueConcurrencyType
    [__managedObjectContext setParentContext:[self primaryObjectContext]];
    return __managedObjectContext;
}
Run Code Online (Sandbox Code Playgroud)

表视图控制器提供了一个模态视图控制器来添加新记录,但使用单独的托管对象上下文来执行此操作(此上下文是父上下文的另一个子级).此上下文保存在表视图控制器的委托回调中:

- (void)addGame
{
    // New child context

    [self setBuildManagedObectContext:[[NSManagedObjectContext alloc] init]];
    [[self buildManagedObectContext] setParentContext:[[[self team] managedObjectContext] parentContext]];

    Team *buildTeam = (Team *)[[self buildManagedObectContext] objectWithID:[[self team] objectID]];
    Game *buildGame = [NSEntityDescription insertNewObjectForEntityForName:@"Game" 
                                                inManagedObjectContext:[self buildManagedObectContext]];


    [buildGame setTeam:buildTeam];

    BuildViewController *buildVC = [[BuildViewController alloc] initWithGame:buildGame delegate:self];
    UINavigationController …
Run Code Online (Sandbox Code Playgroud)

core-data objective-c ios5

9
推荐指数
1
解决办法
8870
查看次数

什么是使用NSManagedObjectContext的objectWithID的"正确"方法:

文档说明:

...此方法始终返回一个对象.假定objectID表示的持久性存储中的数据存在 - 如果不存在,则在访问任何属性时(即,触发错误时),返回的对象将引发异常.此行为的好处是它允许您创建和使用故障,然后在以后或在单独的上下文中创建基础行.

在Apple的'Core Recipes'示例应用程序中,该方法的结果用于填充NSFetchRequest,然后使用请求的结果,并对此结果进行注释:

 // first get the object into the context
 Recipe *recipeFault = (Recipe *)[context objectWithID:objectID];

 // this only creates a fault, which may NOT resolve to an object (for example, if the ID is for
 // an objec that has been deleted already):  create a fetch request to get the object for real
 NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
 [request setEntity: [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:context]];
 NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(self == %@)", recipeFault];
                    [request …
Run Code Online (Sandbox Code Playgroud)

cocoa core-data nsmanagedobjectcontext

6
推荐指数
1
解决办法
3770
查看次数

在多个线程上读/写自定义对象

我需要能够从Core Data中获取对象并将它们保存在内存中的可变数组中,以避免不断获取和降低UI/UX.问题是我抓住了其他线程上的对象.我也有时在其他线程上写这些对象.因此,我不能只是保存NSManagedObjects在一个数组中,只是调用类似的东西,myManagedObjectContext.performBlock或者myObject.managedObjectContext.PerformBlock因为你不应该在线程之间传递MOC.

我正在考虑使用自定义对象将我需要的数据从CD对象中抛出.这感觉有点愚蠢,因为我已经为实体创建了Model/NSManagedObject类,并且因为自定义对象是可变的,所以它仍然不是线程安全的.这意味着我必须为多个线程上的对象操作执行类似串行队列的操作?因此,例如,每当我想要读/写/删除对象时,我必须将它扔到我的对象serialQueue中.

这一切看起来真的很讨厌所以我想知道这个问题有什么常见的设计模式或类似的东西吗?有没有更好的方法呢?

multithreading core-data grand-central-dispatch nsmanagedobject swift

0
推荐指数
1
解决办法
320
查看次数