2011-10-14 35 views
6

मैं सीखने की कोशिश कर रहा हूं कि iCloud को coredata के साथ कैसे उपयोग करें। मेरा लक्ष्य मैक/आईओएस पर एक डेटाबेस फ़ाइल को सिंक करना है। मेरे मैक और आईओएस दोनों ऐप्स iCloud को अपडेट कर रहे हैं क्योंकि मैं इसे सिस्टम प्राथमिकताएं-> iCloud-> प्रबंधित करें ... अज्ञात नाम का एक ऐप है जो हर बार जब मैं आईओएस/मैक ऐप पर कुछ बदलता हूं तो बड़ा और बड़ा हो रहा है। इसलिए मुझे लगता है कि ऐप्स क्लाउड में बदलाव संग्रहीत कर रहे हैं। समस्या यह है कि, जब मैं दूसरे प्लेटफ़ॉर्म पर कुछ बदलता हूं तो मुझे NSPersistentStoreDidImportUbiquitousContentChangesNotification नहीं मिल रहा है।मुझे NSPersistentStoreDidImportUbiquitousContentChangesNotification (कोड नमूना के साथ) नहीं मिल रहा है

मेरे कोड (मैक अनुप्रयोग) है:

- (void)persistentStoreDidImportUbiquitousContentChangesNotification:(NSNotification *)notif 
{ 
    NSLog(@"%@", notif); 
} 

- (NSURL *)applicationFilesDirectory 
{ 
    NSString *identifier = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSURL *libraryURL = [[[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:identifier]; 

    if(![fileManager fileExistsAtPath:[libraryURL path]]) 
    { 
     [fileManager createDirectoryAtPath:[libraryURL path] withIntermediateDirectories:YES attributes:nil error:nil]; 
    } 

    return libraryURL; 
} 

- (NSManagedObjectModel *)managedObjectModel 
{ 
    if (__managedObjectModel) 
    { 
     return __managedObjectModel; 
    } 

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"momd"]; 
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];  
    return __managedObjectModel; 
} 

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (__persistentStoreCoordinator) 
    { 
     return __persistentStoreCoordinator; 
    } 

    NSManagedObjectModel *mom = [self managedObjectModel]; 
    if (!mom) 
    { 
     NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd)); 
     return nil; 
    } 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSURL *applicationFilesDirectory = [self applicationFilesDirectory]; 
    NSError *error = nil; 

    NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:[NSArray arrayWithObject:NSURLIsDirectoryKey] error:&error]; 

    if (!properties) 
    { 
     BOOL ok = NO; 
     if ([error code] == NSFileReadNoSuchFileError) 
     { 
      ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error]; 
     } 
     if (!ok) 
     { 
      [[NSApplication sharedApplication] presentError:error]; 
      return nil; 
     } 
    } 
    else 
    { 
     if ([[properties objectForKey:NSURLIsDirectoryKey] boolValue] != YES) 
     { 
      NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]]; 

      NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
      [dict setValue:failureDescription forKey:NSLocalizedDescriptionKey]; 
      error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict]; 

      [[NSApplication sharedApplication] presentError:error]; 
      return nil; 
     } 
    } 

    NSString *appBundleID = [[NSBundle mainBundle] bundleIdentifier]; 
    NSURL *storeURL = [applicationFilesDirectory URLByAppendingPathComponent:@"MyApp.sqlite"]; 
    NSURL *cloudURL = [[fileManager URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Database"]; 

    NSPersistentStoreCoordinator *coordinator = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom] autorelease]; 
    NSDictionary *optionsDict = [NSDictionary dictionaryWithObjectsAndKeys:appBundleID, NSPersistentStoreUbiquitousContentNameKey, cloudURL, NSPersistentStoreUbiquitousContentURLKey, [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,nil]; 
    if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:optionsDict error:&error]) 
    { 
     [[NSApplication sharedApplication] presentError:error]; 
     return nil; 
    } 
    __persistentStoreCoordinator = [coordinator retain]; 

    return __persistentStoreCoordinator; 
} 

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

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (!coordinator) 
    { 
     NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
     [dict setValue:@"Failed to initialize the store" forKey:NSLocalizedDescriptionKey]; 
     [dict setValue:@"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey]; 
     NSError *error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict]; 
     [[NSApplication sharedApplication] presentError:error]; 
     return nil; 
    } 
    __managedObjectContext = [[NSManagedObjectContext alloc] init]; 
    [__managedObjectContext setPersistentStoreCoordinator:coordinator]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(persistentStoreDidImportUbiquitousContentChangesNotification:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator]; 

    return __managedObjectContext; 
} 
+0

मुझे पता चला उन फ़ाइलों बादल में कर रहे हैं: ".baseline", ".cdmetadata.nosync", "kukosk.F8533032-7CA2-5A89-8097-62CD3D1C2FA3", "mobile.CB9C2446-D756- 575E-A553-7ABCFD2D369F " – Kukosk

उत्तर

1

मैं iOS SDK के साथ एक ही समस्या का सामना करना पड़ा।

प्राप्त करने के लिए "NSPersistentStoreDidImportUbiquitousContentChangesNotification" अधिसूचना, आप कॉल करना होगा "- (शून्य) addObserver: (आईडी) पर्यवेक्षक चयनकर्ता: (SEL) aSelector नाम: (NSString *) aName वस्तु: (आईडी) anObject" NSNotification केंद्र से।

मैं AppDelegate की "didFinishLaunchingWithOptions" विधि में इस लाइन

[[NSNotificationCenter defaultCenter] addObserver:masterViewController selector:@selector(notifyiCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:[self persistentStoreCoordinator]];

गयी। मेरे मामले में, (आईओएस 5) यह पूरी तरह से काम करता है, लेकिन मुझे नहीं पता कि यह मैक ओएस में भी काम करेगा या नहीं।

0

मुझे आईओएस और मैकोज़ एक्स दोनों पर चल रहे कोरडाटा स्टैक के साथ एक ही समस्या हो रही थी। मेरे आईफोन और आईपैड के बीच, एनएसपीर्सिस्टेंटस्टोरडिड इंपोर्ट यूबिविटीस कंटेंट चेंज नोटिफिकेशन: ठीक काम किया।

मैक पर मैंने समय-समय पर एनएसएफएटीएचआरक्वेट निष्पादित किया (प्रत्येक 5 सेकंड कहें) और परिणाम को त्याग दिया। केवल तभी अधिसूचना आ गई।

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