1

2番目のNSManagedObjectContextにコアデータオブジェクトを追加しようとすると、異なるコンテキストのオブジェクト間に関係'info'を確立しようとして不正な試みが行われたというエラーが表示されます。ただし、2つのエンティティは同じコンテキストにあります(少なくとも、そうする必要があります)。デバッガーは同じ16進数を提供します。このコードの何が問題になっていますか?

-(void) loadHistoricalPrices
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Create context on background thread
if(document!=NULL)
{
    NSManagedObjectContext *context = NULL;
    NSNotificationCenter *nc =NULL;

    context = [[NSManagedObjectContext alloc] init];
    [context setUndoManager:nil];
    [context setPersistentStoreCoordinator: [document.managedObjectContext persistentStoreCoordinator]];


    // Register context with the notification center
    nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(mergeChanges:)
               name:NSManagedObjectContextDidSaveNotification
             object:context];

    NSEntityDescription * infoEntity;
    infoEntity = [NSEntityDescription entityForName:@"Info" inManagedObjectContext:context];
    NSEntityDescription * historyEntity;
    historyEntity = [NSEntityDescription entityForName:@"History" inManagedObjectContext:context];



// fetching an object-array of info-entity, called result

    if([result count]>0)
    {

        NSArray * items;
       // getting items to insert into history-entity


        for(NSString * item in items)
        {
            NSArray * subs;
            subs=[item componentsSeparatedByString:@","];
            if([subs count]>5)
            {
                    HistoryMO * newHistory;

                    newHistory = [[[HistoryMO alloc] initWithEntity:historyEntity insertIntoManagedObjectContext:context] autorelease];
                    [newHistory setValue:[NSNumber numberWithDouble:[[subs objectAtIndex:4] doubleValue]] forKey:@"course"];
[newHistory setValue:infoMO forKey:@"info"];

// >>>>> CRASHES HERE

            }                
        }
    }


    // merge data
    if([[[context persistentStoreCoordinator] persistentStores] count]>0)
    {
        NSLog(@"saving...");

        error=NULL;
        [context save:&error];
        if (!error) [context reset]; else NSLog(@"error saving historic prices %@",[error localizedDescription]);
        if (!error) NSLog(@"saved successfully");
    }

    [nc removeObserver:self];
    [context release];
}
[pool release];
}
4

3 に答える 3

1

infoMOが同じコンテキストからのものである場合、次の行で[contextreset]を呼び出した後に無効になります。

if (!error) [context reset]; else NSLog(@"error saving historic prices %@",[error localizedDescription]);

したがって、それを再フェッチする必要があります。これが、ChrisHのソリューションが機能した理由です。

于 2012-12-06T13:01:40.100 に答える
0

どこinfoMOから来たの?// getting items to insert into history-entityコメントがあるところで起こっているようです。私の推測では、これらのオブジェクトをフェッチするために別のコンテキストを使用しています。infoMOに挿入するために使用しているものと同じコンテキストを使用してオブジェクトを検索する必要がHistoryMOあります。そうしないと、コンテキストが関係を管理できません。

于 2012-10-01T14:19:52.897 に答える
0

コードサンプルからはわかりにくいですが、クラッシュ情報に基づいて、infoMOオブジェクトは別のコンテキストからのものです。異なるコンテキストの 2 つのオブジェクト間に関係を作成することはできません。

最も簡単な解決策は、NSManagedObject の objectID プロパティを使用して、現在のコンテキストで infoMO への参照を取得することです。

infoMO インスタンスが Info クラスであると仮定すると、次のようになります。

Info *contextInfoMO = [context objectWithID:infoMO.objectID];
[newHistory setValue:contextInfoMO forKey:@"info"];
于 2012-10-01T16:37:01.523 に答える