2011-11-16 13 views
6

मैं एक आईफोन ऐप और एक मैक ऐप काम कर रहा हूं जो कोर डेटा का उपयोग करता है।मैक/आईफोन ऐप - iCloud और उपकरणों (कोर डेटा का उपयोग करके) को कोर डेटा सिंक करना

मैं इन 2 ऐप्स को अपने डेटाबेस को iCloud संग्रहण के माध्यम से सिंक्रनाइज़ करना चाहता हूं।

मैं के कार्यान्वयन के लिए समायोजन कर दिया है managedObjectContext & persistentStoreCoordinator & जोड़ा mergeiCloudChanges - अद्यतन व्यंजनों उदाहरण कोड से:

#pragma mark - 
#pragma mark Core Data stack 

// this takes the NSPersistentStoreDidImportUbiquitousContentChangesNotification 
// and transforms the userInfo dictionary into something that 
// -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:] can consume 
// then it posts a custom notification to let detail views know they might want to refresh. 
// The main list view doesn't need that custom notification because the NSFetchedResultsController is 
// already listening directly to the NSManagedObjectContext 
- (void)mergeiCloudChanges:(NSNotification*)note forContext:(NSManagedObjectContext*)moc { 

    NSLog(@"merging iCloud stuff"); 

    [moc mergeChangesFromContextDidSaveNotification:note]; 

    NSNotification* refreshNotification = [NSNotification notificationWithName:@"RefreshAllViews" object:self userInfo:[note userInfo]]; 

    [[NSNotificationCenter defaultCenter] postNotification:refreshNotification]; 
} 

/** 
Returns the managed object context for the application. 
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
*/ 
- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (managedObjectContext != nil) 
    { 
     return managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 

    if (coordinator != nil) 
    { 
     if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) { 
      NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; 

      [moc performBlockAndWait:^{ 
       [moc setPersistentStoreCoordinator: coordinator]; 

       [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(mergeChangesFrom_iCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator]; 
      }]; 
      managedObjectContext = moc; 
     } else { 
      managedObjectContext = [[NSManagedObjectContext alloc] init]; 
      [managedObjectContext setPersistentStoreCoordinator:coordinator]; 
     } 

    } 
    return managedObjectContext; 
} 

// NSNotifications are posted synchronously on the caller's thread 
// make sure to vector this back to the thread we want, in this case 
// the main thread for our views & controller 
- (void)mergeChangesFrom_iCloud:(NSNotification *)notification { 


    NSManagedObjectContext* moc = [self managedObjectContext]; 

    // this only works if you used NSMainQueueConcurrencyType 
    // otherwise use a dispatch_async back to the main thread yourself 
    [moc performBlock:^{ 
     [self mergeiCloudChanges:notification forContext:moc]; 
    }]; 
} 


/** 
Returns the managed object model for the application. 
If the model doesn't already exist, it is created by merging all of the models found in the application bundle. 
*/ 
- (NSManagedObjectModel *)managedObjectModel { 

    if (managedObjectModel != nil) { 
     return managedObjectModel; 
    } 
    managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];  
    return managedObjectModel; 
} 





- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator__ != nil) { 
     return persistentStoreCoordinator__; 
    } 

    // assign the PSC to our app delegate ivar before adding the persistent store in the background 
    // this leverages a behavior in Core Data where you can create NSManagedObjectContext and fetch requests 
    // even if the PSC has no stores. Fetch requests return empty arrays until the persistent store is added 
    // so it's possible to bring up the UI and then fill in the results later 
    persistentStoreCoordinator__ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 


    // prep the store path and bundle stuff here since NSBundle isn't totally thread safe 
    NSPersistentStoreCoordinator* psc = persistentStoreCoordinator__; 
    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"MyApp.sqlite"]; 

    // do this asynchronously since if this is the first time this particular device is syncing with preexisting 
    // iCloud content it may take a long long time to download 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 
     // this needs to match the entitlements and provisioning profile 
     NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil]; 
     NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"MyApp"]; 
     cloudURL = [NSURL fileURLWithPath:coreDataCloudContent]; 

     NSLog(@"cloudURL: %@", cloudURL);   

     // The API to turn on Core Data iCloud support here. 
     NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:@"xxxxxxxx.com.me.MyApp", 
           @"MyApp", 
           cloudURL, 
           NSPersistentStoreUbiquitousContentURLKey, 
           [NSNumber numberWithBool:YES], 
           NSMigratePersistentStoresAutomaticallyOption, 
           [NSNumber numberWithBool:YES], 
           NSInferMappingModelAutomaticallyOption, 
           nil]; 

     NSError *error = nil; 

     [psc lock]; 
     if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
      /* 
      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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

      Typical reasons for an error here include: 
      * The persistent store is not accessible 
      * The schema for the persistent store is incompatible with current managed object model 
      Check the error message to determine what the actual problem was. 
      */ 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     }  
     [psc unlock]; 

     // tell the UI on the main thread we finally added the store and then 
     // post a custom notification to make your views do whatever they need to such as tell their 
     // NSFetchedResultsController to -performFetch again now there is a real store 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      NSLog(@"asynchronously added persistent store!"); 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil]; 
     }); 
    }); 

    return persistentStoreCoordinator__; 
} 

मैं फ़ाइलों को अपने "/ उपयोगकर्ताओं/मुझे/लाइब्रेरी/मोबाइल में दिखाई देते हैं देख सकते हैं दस्तावेज "निर्देशिका जब मैं myapp बना/चलाता हूं।
लेकिन मुझे नहीं पता कि यह iCloud संग्रहण पर समन्वयित हो रहा है - और स्पष्ट रूप से आईफोन और मैक के बीच डेटा सिंक नहीं किया गया है।
क्या क्लाउड में डेटा स्थानांतरित करने के लिए मुझे अन्य विधियों को लागू करने की आवश्यकता है?
और क्या मेरे लिए यह देखने का कोई तरीका है कि वास्तव में iCloud संग्रहण पर कौन से दस्तावेज़ हैं?

उत्तर

3

यहां एक त्वरित आंशिक उत्तर दिया गया है।

आप देख सकते हैं क्या iCloud में संग्रहीत किया जाता है:

मैक:

सिस्टम Preferences.app -> iCloud -> पर क्लिक करें 'प्रबंधित करें ...' आप तो सभी की एक सूची दिखाई देगी ऐप जिनके पास मैक ओएस एक्स या आईओएस संग्रहीत दस्तावेज़ हैं।

iOS पर:

प्राथमिकताएं -> iCloud -> पुरालेख & बैकअप ->विकल्प अंतरिक्ष नीचे इस्तेमाल किया आप तो संग्रहीत मैक ओएस एक्स या iOS दस्तावेजों है कि सभी एप्लिकेशन की सूची दिखाई देगी।

जब तक आप NSFileManager के setUbiquitous: itemAtURL: destinationURL: error: का उपयोग कर रहे हैं, तो दस्तावेजों को आपके लिए iCloud पर भेजा जाना चाहिए और अन्य उपकरणों पर दिखाना चाहिए।

+0

हाय माइक, आपके उत्तर के लिए धन्यवाद। मैंने sys prefs के माध्यम से अपने iCloud संग्रहण की जांच की है। ऐसा लगता है कि कुछ भी कॉपी नहीं हो रहा है। अच्छी तरह से देख रहे हैं। कोई अन्य सुझाव? इसके अलावा हाँ मैं NSFileManager के setUbiquitous – adamteale

+0

आह उपयोग कर रहा हूँ वास्तव में कोई मैं उपयोग नहीं कर रहा "setUbiquitous: itemAtURL: DESTINATIONURL: त्रुटि:" - मैं "addPersistentStoreWithType" का उपयोग कर रहा है और यह विकल्प की एक शब्दकोश जो NSPersistentStoreUbiquitousContentNameKey शामिल गुजर यह एक ही विचार है ? मैंने पढ़ा होगा ... धन्यवाद फिर से – adamteale

+0

मैंने देखा है कि मेरे ऐप इस लाइन पर लटका हुआ है: 'अगर ([[NSFileManager defaultManager] setUbiquitous: makeUbiquitous itemAtURL: fileURL DESTINATIONURL: destURL त्रुटि: और अं]) {' इसलिए मैं ऐप को एक्सकोड से रोकता हूं, और फिर यह कंसोल ऐप में दिखाई देता है: 'लाइब्ररैंड: क्लाइंट कनेक्शन अमान्य है: कनेक्शन अमान्य' कोई विचार? – adamteale

1

एक और आंशिक उत्तर। यदि आपने अभी तक नहीं किया है तो http://www.raywenderlich.com/6015/beginning-icloud-in-ios-5-tutorial-part-1 पर एक नज़र डालें। मैं वही चीज़ पर काम कर रहा हूं जैसे मैक ऐप और आईओएस एप साझाकरण डेटा। शुभ लाभ। मार्क

मैंने अभी इसके बारे में सीखा: http://mentalfaculty.tumblr.com/archive "शीट्स के तहत" कोरडाटा और आईक्लाउड के लिए देखें। इसकी जांच - पड़ताल करें!

0

ठीक है मेरा कोड थोड़ा अलग दिखता है, मेरे पास अपनी सभी परियोजनाओं के लिए इसे पुन: उपयोग करने के लिए एक अलग वर्ग में है। फिर भी अगर iCloud सक्षम है (URLForUbiquityContainerIdentifier: शून्य नहीं शून्य वापसी) इस तरह मैं सेटअप मेरे NSPersistentStoreCoordinator:

// ---- iCloud Setup 

// fist container in entitlements 
NSURL *iCloudDirectoryURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; 

// if iCloud is enabled setup the store 
if (iCloudDirectoryURL) { 

    __iCloudEnabled = true; 

    NSLog(@"iCloud:%@", [iCloudDirectoryURL absoluteString]); 
    // AppDelegate has to provide the contentnamekey 
    NSString *contentNameKey = [appDelegate dataStoreContentNameKey]; 

    options = [NSDictionary dictionaryWithObjectsAndKeys:contentNameKey, NSPersistentStoreUbiquitousContentNameKey, iCloudDirectoryURL, NSPersistentStoreUbiquitousContentURLKey, nil]; 
} 

मैं जहां सेटअप NSPersistentStoreUbiquitousContentNameKey याद आती है।

दूसरा मुझे लगता है कि यह स्पष्ट है, यह केवल डिवाइस पर काम करता है और आपकी ऐप आईडी को iCloud सक्षम होना आवश्यक है।