Eccezione con PFubiquityPeer coinvolto quando si aggiunge un archivio persistente con dati core e iCloud

StackOverflow https://stackoverflow.com/questions/9349998

Domanda

Quando aggiungo un negozio persistente con NSPersistentStoreUbiquitousContentNameKey e NSPersistentStoreUbiquitousContentURLKey La mia applicazione si blocca ogni seconda volta con un'eccezione. Questo accade durante la chiamata a addPersistentStoreWithType:configuration:URL:options:error:

La traccia dello stack sembra così:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
  reason: 'An NSManagedObject of class 'PFUbiquityPeer' must have a valid
  NSEntityDescription.'
*** First throw call stack:
(
     0   CoreFoundation   0x00007fff8b35ffc6 __exceptionPreprocess + 198
     1   libobjc.A.dylib  0x00007fff83368d5e objc_exception_throw + 43
     2   CoreData         0x00007fff82c28c06 -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:] + 182
     3   CoreData         0x00007fff82d4d3dc +[PFUbiquityPeer(UbiquityMethods) peerForPeerID:inManagedObjectContext:createIfMissing:] + 364
     4   CoreData         0x00007fff82d4f809 -[PFUbiquityPeerRange(UbiquityMethods) loadFromStoreMetadataDictionary:] + 105
     5   CoreData         0x00007fff82d80372 -[PFUbiquityStoreMetadataMedic recoverPeerRangesWithError:] + 418
     6   CoreData         0x00007fff82d80ab5 -[PFUbiquityStoreMetadataMedic recoverMetadataWithError:] + 1749
     7   CoreData         0x00007fff82d831dc -[PFUbiquitySetupAssistant performPostStoreSetupWithStore:error:] + 732
     8   CoreData         0x00007fff82c04001 -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] + 3537
     9   MyApp            0x000000010323de60 -[AppDelegate persistentStoreCoordinator] + 4224
     10  MyApp            0x000000010323e43f -[AppDelegate managedObjectContext] + 95
     11  Foundation       0x00007fff863b5384 _NSGetUsingKeyValueGetter + 62
     12  Foundation       0x00007fff863b5339 -[NSObject(NSKeyValueCoding) valueForKey:] + 392
     13  Foundation       0x00007fff863d4dc6 -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 348
     14  AppKit           0x00007fff87fb1ae2 -[NSBinder _valueForKeyPath:ofObject:mode:raisesForNotApplicableKeys:] + 654
     15  AppKit           0x00007fff87fb17cc -[NSBinder valueForBinding:resolveMarkersToPlaceholders:] + 171
     16  AppKit           0x00007fff87fb143a -[NSObjectParameterBinder _updateObject:observedController:observedKeyPath:context:] + 1181
     17  AppKit           0x00007fff87fa3777 -[NSObject(NSKeyValueBindingCreation) bind:toObject:withKeyPath:options:] + 591
     18  AppKit           0x00007fff87f9ca89 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1079
     19  AppKit           0x00007fff87f9309f loadNib + 322
     20  AppKit           0x00007fff87f9259c +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
     21  AppKit           0x00007fff87f924b7 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
     22  AppKit           0x00007fff87f923fa +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
     23  AppKit           0x00007fff882059b3 NSApplicationMain + 398
     24  MyApp            0x0000000103239522 main + 34
     25  MyApp            0x00000001032394f4 start + 52
     26  ???              0x0000000000000003 0x0 + 3
)
terminate called throwing an exception(lldb)

Quando non aggiungo il NSPersistentStoreUbiquitousContentNameKey e NSPersistentStoreUbiquitousContentURLKey Opzioni per il negozio tutto funziona senza problemi. Ricevo questa eccezione solo su ogni secondo lancio dell'applicazione. Ai fini del test (e poiché pensavo che il problema potesse avere qualcosa a che fare con la concorrenza), ho rimosso tutto l'accesso ai dati core e gli assistenti dal file NIB principale e ho provato ad accedere a core stack di dati programmaticamente all'interno del applicationDidFinishLaunching: metodo. Nel primo test l'ho fatto direttamente, la seconda volta che ho eseguito il selettore (managedObjectContext) dopo un ritardo di 10 secondi. Entrambi i test hanno comportato la stessa eccezione su ogni secondo lancio dell'applicazione.

Il (leggermente abbreviato per questo esempio) managedObjectContext Il metodo sembra così:

- (NSManagedObjectContext *)managedObjectContext
{
  if (managedObjectContext_)
  {
    return managedObjectContext_;
  }

  NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator;
  if (!coordinator)
  {
    return nil;
  }

  NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] 
    initWithConcurrencyType:NSMainQueueConcurrencyType];
  [moc performBlockAndWait:
   ^{
    moc.mergePolicy = [[NSMergePolicy alloc] 
      initWithMergeType:NSMergeByPropertyObjectTrumpMergePolicyType];
    [moc setPersistentStoreCoordinator:coordinator];

    [[NSNotificationCenter defaultCenter] addObserver:self
      selector:@selector(mergeChangesFrom_iCloud:) 
      name:NSPersistentStoreDidImportUbiquitousContentChangesNotification 
      object:coordinator];
  }];
  managedObjectContext_ = moc;

  return managedObjectContext_;
}

La parte rilevante di persistentStoreCoordinator è:

  NSMutableDictionary *storeOptions = [NSMutableDictionary dictionary];
  [storeOptions setObject:[NSNumber numberWithBool:YES] 
    forKey:NSMigratePersistentStoresAutomaticallyOption];
  [storeOptions setObject:[NSNumber numberWithBool:YES] 
    forKey:NSInferMappingModelAutomaticallyOption];

  NSURL *url = [NSURL fileURLWithPath:[applicationSupportDirectory 
    stringByAppendingPathComponent:kJCMyAppDatabaseFilename]];
  NSURL *ubiquityURL = [[NSFileManager defaultManager] 
    URLForUbiquityContainerIdentifier:nil];
  if (ubiquityURL)
  {
    JCDLog(@"User has iCloud enabled.");
    [storeOptions setObject:@"com.juicycocktail.myapp" 
      forKey:NSPersistentStoreUbiquitousContentNameKey];
    [storeOptions setObject:[NSURL fileURLWithPath:[[ubiquityURL path] 
      stringByAppendingPathComponent:kJCMyAppDatabaseFilename]] 
      forKey:NSPersistentStoreUbiquitousContentURLKey];       
  }

  persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] 
    initWithManagedObjectModel:mom];
  NSPersistentStoreCoordinator *psc = persistentStoreCoordinator_;

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
  (dispatch_block_t)^{
    [psc lock];
    if (![psc addPersistentStoreWithType:kJCMyAppStoreType
      configuration:nil URL:url options:storeOptions error:&error])
    {
      dispatch_async(dispatch_get_main_queue(),
      ^{
        [[NSApplication sharedApplication] presentError:error];
        persistentStoreCoordinator_ = nil;

        return;
      });
    }    
    [psc unlock];

    dispatch_async(dispatch_get_main_queue(),
    ^{
      JCDLog(@"asynchronously added persistent store!");
      [[NSNotificationCenter defaultCenter]
        postNotificationName:@"RefetchAllDatabaseData"
        object:self userInfo:nil];
    });
  });

  return persistentStoreCoordinator_;

Immagino che la classe PFubiquityPeer potrebbe avere qualcosa a che fare con i registri delle transazioni di Core Data che sono conservati nella cartella dei documenti mobili di iCloud, ma non riesco ancora a trovare la vera causa di questo problema. Qualsiasi aiuto come tracciare la radice di questa eccezione è molto apprezzato perché sto già diventando pazzo. Soprattutto una soluzione alternativa o addirittura una soluzione sarebbe molto utile per me. Sono anche contento che qualcuno abbia un suggerimento su come rintracciare questa eccezione.

Nota: ho anche presentato un bug nel caso in cui si tratti di un bug API (RDAR: // 10892613).

È stato utile?

Soluzione

Il riavvio di OS X ha risolto il problema. Questo mi ricorda un Crolla che corre il bavaglio.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top