2010-06-11 11 views
19

का पता लगाने मैं कोर डेटा के स्वचालित हल्के माइग्रेशन का सफलतापूर्वक उपयोग कर रहा हूं। हालांकि, जब माइग्रेशन के दौरान कोई विशेष इकाई बनाई जाती है, तो मैं इसे कुछ डेटा के साथ पॉप्युलेट करना चाहता हूं। निस्संदेह मैं यह जांच सकता हूं कि आवेदन शुरू होने पर इकाई खाली होती है, लेकिन जब कोर डेटा में माइग्रेशन फ्रेमवर्क होता है तो यह अक्षम होता है।लाइटवेट कोर डेटा माइग्रेशन

क्या यह पता लगाना संभव है कि हल्का माइग्रेशन कब होता है (संभवतः केवीओ या अधिसूचनाओं का उपयोग करके), या क्या इसे मानक माइग्रेशन को लागू करने की आवश्यकता होती है?

मैंने NSPersistentStoreCoordinatorStoresDidChangeNotification का उपयोग करने का प्रयास किया है, लेकिन माइग्रेशन होने पर यह आग नहीं होती है।

उत्तर

54

पता लगाने के लिए एक प्रवास की जरूरत है या नहीं, अगर लगातार दुकान समन्वयक के प्रबंधित ऑब्जेक्ट मॉडल मौजूदा दुकान के मेटाडाटा के साथ संगत है देखने के लिए जाँच (एप्पल के Is Migration Necessary से रूपांतरित):

NSError *error = nil; 
persistentStoreCoordinator = /* Persistent store coordinator */ ; 
NSURL *storeUrl = /* URL for the source store */ ; 

// Determine if a migration is needed 
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType 
                          URL:storeUrl 
                         error:&error]; 
NSManagedObjectModel *destinationModel = [persistentStoreCoordinator managedObjectModel]; 
BOOL pscCompatibile = [destinationModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata]; 
NSLog(@"Migration needed? %d", !pscCompatibile); 

तो pscCompatibileNO है, तो एक प्रवासन होने की आवश्यकता होगी। इकाई परिवर्तन की जांच करने के लिए, [destinationModel entities] करने के लिए sourceMetadata शब्दकोश में NSStoreModelVersionHashes कुंजी तुलना:

NSSet *sourceEntities = [NSSet setWithArray:[(NSDictionary *)[sourceMetadata objectForKey:@"NSStoreModelVersionHashes"] allKeys]]; 
NSSet *destinationEntities = [NSSet setWithArray:[(NSDictionary *)[destinationModel entitiesByName] allKeys]]; 

// Entities that were added 
NSMutableSet *addedEntities = [NSMutableSet setWithSet:destinationEntities]; 
[addedEntities minusSet:sourceEntities]; 

// Entities that were removed 
NSMutableSet *removedEntities = [NSMutableSet setWithSet:sourceEntities]; 
[removedEntities minusSet:destinationEntities]; 

NSLog(@"Added entities: %@\nRemoved entities: %@", addedEntities, removedEntities); 
+1

+1। – cocoafan

+0

@ हैड्रोनज़ू यह हमेशा माइग्रेट कर रहा है क्योंकि मैं एप्लिकेशन शुरू करता हूं, नहीं चाहिए 'यह केवल एक बार किया जाना चाहिए? –

+0

@AhmedZ। नहीं, यह सिर्फ एक बार होता है –

1

क्या उस संस्था के लिए NSManagedObject उपवर्गीकरण, और फिर -awakeFromInsert अधिभावी के बारे में :? या आप इस ऐप को अपने ऐप के अन्य हिस्सों में बना रहे हैं? आपके उत्तर के दूसरे भाग को साझा करने के लिए

संबंधित मुद्दे