2015-07-15 27 views
5

Trong ứng dụng của tôi cho iOS, tôi đã đặt cơ sở dữ liệu của mình để đồng bộ hóa với iCloud. Khi tôi chèn một bản ghi mới dữ liệu được cập nhật chính xác và tôi có thể xem nó tại thư mục chứa của tôi. Bây giờ tôi muốn đặt một chuyển đổi để cho phép người dùng bật hoặc tắt đồng bộ hóa dữ liệu cốt lõi với iCloud trong khi ứng dụng đang chạy, nhưng tôi không biết cách thực hiện việc này.iOS ICloud Core Data switch tắt từ ứng dụng

Tôi mới sử dụng iOS và ICloud. Cần giúp đỡ xin vui lòng. Cảm ơn trước.

Đây là mã của tôi trong appdelegate:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (_persistentStoreCoordinator != nil) { 
     return _persistentStoreCoordinator; 
    } 

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myStore.sqlite"];  
    NSURL *cloudRootURL=[fileManager URLForUbiquityContainerIdentifier:nil]; 

    NSString *pathToCloudFile = [[cloudRootURL path]stringByAppendingPathComponent:@"Documents"]; 
    pathToCloudFile = [pathToCloudFile stringByAppendingPathComponent:@"myCloudLogs"]; 

    NSURL *cloudURL = [NSURL fileURLWithPath:pathToCloudFile]; 

    NSString *cloudStoreTitle = @"myStoreCloud"; 
    NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES, 
          NSInferMappingModelAutomaticallyOption : @YES, 
          NSPersistentStoreUbiquitousContentURLKey: cloudURL, 
          NSPersistentStoreUbiquitousContentNameKey: cloudStoreTitle}; 

    NSError *error = nil; 
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 


    if (![_persistentStoreCoordinator 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. 

     //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. 


     //If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 

     //If you encounter schema incompatibility errors during development, you can reduce their frequency by: 
     // Simply deleting the existing store: 
     //[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 

     // Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
     //@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} 

     //Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 


     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; 

    [notificationCenter addObserver:self 
         selector:@selector(processStoresWillChange:) 
          name:NSPersistentStoreCoordinatorStoresWillChangeNotification 
         object:_persistentStoreCoordinator]; 

    [notificationCenter addObserver:self 
         selector:@selector(processStoresDidChange:) 
          name:NSPersistentStoreCoordinatorStoresDidChangeNotification 
         object:_persistentStoreCoordinator]; 

    [notificationCenter addObserver:self 
         selector:@selector(processContentChanges:) 
          name:NSPersistentStoreDidImportUbiquitousContentChangesNotification 
         object:_persistentStoreCoordinator]; 


    return _persistentStoreCoordinator; 
} 
+0

Tôi không nghĩ bạn có thể làm được. Trên thực tế sự hiểu biết của tôi là, tôi có một persistentStore mà là hoặc iCloud được kích hoạt hay không (tức là bạn xác định nó khi thiết lập với storeOptions là nil hoặc có chứa một giá trị). Vì vậy, tôi sẽ đề nghị, rằng bạn phải lưu tất cả các thay đổi và sau đó thiết lập lại bối cảnh đối tượng được quản lý và loại bỏ persistentStore và thiết lập một persistentStore mới với storeOption = nil (sau đó không bật iCloud). Nếu bạn muốn tôi có thể cung cấp cho bạn một số mã mẫu. – Red

Trả lời

0

Không sử dụng thùng chứa có mặt khắp nơi. Hãy xem tài liệu về apple để loại trừ tệp khỏi đồng bộ hóa icloud. https://developer.apple.com/library/ios/qa/qa1719/_index.html Nếu bạn đặt giá trị bool thành NO. Các tập tin sẽ được một lần nữa bao gồm trong các tập tin để sao lưu. Tạo dữ liệu cốt lõi (cơ sở dữ liệu) trong tài liệu để nó tự động đồng bộ hóa. Sử dụng mã này, bạn có thể tắt đồng bộ hóa lập trình bất cứ lúc nào bằng cách nhấp vào nút. Nhưng được cảnh báo rằng ứng dụng có thể bị từ chối nếu kích thước cơ sở dữ liệu phát triển quá nhiều, bởi vì chỉ "dữ liệu người dùng" nên được đồng bộ hóa với icloud.

Các vấn đề liên quan