2015-08-18 34 views
19

Ben Core Data Stack in Swift - Demystified içinden çalışıyordu ama ben çizgiNSManagedObjectContext(): `init()` iOS 9.0 kullanımdan kaldırıldı: Kullanım -initWithConcurrencyType

self.context = NSManagedObjectContext() 

var ben uyarı var

`init()` was deprecated in iOS 9.0: Use -initWithConcurrencyType: instead 

Ben self.context =

NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.ConfinementConcurrencyType) 
NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType) 
NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType) 

için değil,beri aşağıdakilerden birini yapabilirsiniz görüyoruz Ayrıca şimdi MainQueueConcurrencyType ve PrivateQueueConcurrencyType'u bırakankullanımdan kaldırıldı. Bu ikisi arasındaki fark nedir ve hangisini kullanmayı seçmeliyim? this documentation'u okudum, ama gerçekten anlamadım.

+1

Belki [Konuyu] (http://stackoverflow.com/questions/8637921/core-datas-nsprivatequeueconcurrencytype-and-sharing-objects-between -threads) size yardımcı olur. – Jens

cevap

21

Esasen her zaman NSMainQueueConcurrencyType ile en az 1 içerik ve NSPrivateQueueConcurrencyType ile birçok bağlam olacaktır. NSPrivateQueueConcurrencyType genellikle işleri arka plandaki çekirdek verilere kaydetmek veya almak için kullanılır (bir Web Hizmeti ile kayıt senkronize etme denemesi gibi).

NSMainQueueConcurrencyType, NSFetchedResultsController ile kullanım için mükemmel olan ana sırayla ilişkili bir bağlam oluşturur.

Varsayılan çekirdek veri yığını, NSMainQueueConcurrencyType ile tek bir bağlam kullanır, ancak UI'yi etkilemeyen herhangi bir iş yapmak için birden çok NSPrivateQueueConcurrencyType'u kaldırarak çok daha iyi bir uygulama oluşturabilirsiniz.

+0

Cevabınız için teşekkür ederiz. Tüm bağlamlar için 'PrivateQueueConcurrencyType 'kullanabilir miyim? Bunun garip davranışlara neden olabileceğini veya kazaya neden olabileceğini düşünüyorum. – sahara108

+1

Ana sıranın üzerinde yapılması gereken herhangi bir iş için 'PrivateQueueConcurrencyType' kullanılmamalıdır. Her zaman 'MainQueueConcurrencyType'ı' NSFetchedResultsController' ile kullanmalısınız. Bir "PrivateQueueConcurrencyType" bir arka plan iş parçacığı kullanır, bu nedenle bu fikirleri saklayın. – DBoyer

7

aşağıdaki biriyle bu iki işlevi yerine:

lazy var managedObjectContext: NSManagedObjectContext = { 
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail. 
    let coordinator = self.persistentStoreCoordinator 
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) 
    managedObjectContext.persistentStoreCoordinator = coordinator 
    return managedObjectContext 
    }() 

// MARK: - Core Data Saving support 

func saveContext() { 
    if managedObjectContext.hasChanges { 
     do { 
      try managedObjectContext.save() 
     } catch { 
      // Replace this implementation with code to handle the error appropriately. 
      // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
      let nserror = error as NSError 
      NSLog("Unresolved error \(nserror), \(nserror.userInfo)") 
      abort() 
     } 
    } 
}