iCloud

3.1 iCloud入门

  1. iCloud的目标:不是将内容存储在云端,从而释放设备本地的空间。与之相反,iCloud是对本地内容的一个镜像,应用的数据发生了改变,这些改变会被推送到云端,二iCloud会与所有连接上的设备进行同步,将这些修改更新到本地数据库中,保证每个设备都具有同样的本地内容。
  2. 使用场景:只用于存储用户产生的内容(文档、图像、档案、计划等)
    还提供了一个轻量级的键值对存储(与NSUserDefaults类似)64k限制,可以存储用户偏好设置。
  3. 使用步骤:在官网上开启iCloud;添加必要的iCloud授权。
  4. iCloud存储容器一个App ID:H37HMHGFIU.com.anythingsimple.iCloudDemo对应的存储泛存容器为:
    [iCloud]/H37HMHGFIU/com/anythingsimple/iCloudDemo
    如果是iOS,Mac OS X通用的App ID使用:
    com.anythingsimple.iCloudDemo.MacOSX
    这样每个应用存储都在iCloudDemo文件下,可以访问公有内容同时有自己私有的文件夹。


3.2 键值对存储

  1. 使用键值对泛存

     //检查iCloud是否可用
     NSUbiquitousKeyValueStore *store;
         store=[NSUbiquitousKeyValueStore defaultStore];
     if (![store synchronize]) {
         NSLog(@"iCloud is Unavailable");
     }
    
    
  2. 添加和删除对象
    用法与NSUserDefaults类似,调用时把对象调入内存,然后用synchronize将修改提交到磁盘或者云端。不同之处在于使用NSUbiquitousKeyValueStore时iCloud还会通知其他设备发生了这些改动。

     //提交修改
     NSUbiquitousKeyValueStore *store;
     store=[NSUbiquitousKeyValueStore defaultStore];
     [store setObject:favorites forKey:@"favorites"];
     if (![store synchronize]) {
     NSLog(@"can not synchronize favorites");
    
    
  3. 对变更通知做出响应

     //注册通知
     if(iCloudSyncIfAvailable){
             [[NSNotificationCenter defaultCenter] 
             addObserver:self 
             selector:@selector(persistentStoreDidChange:)
             name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
             object:coordinator];
     } 
     //收到通知的响应,注意:这里的代码使用受控对象上下文的mergeChangesFromContextDidSaveNotification的       //方法,和core data交互。本节中键值对存储需从notification中提取变化内容以及原因,代码略
     - (void)persistentStoreDidChange:(NSNotification*)notification{
         NSLog(@"Change Detected!");
         [__managedObjectContext performBlockAndWait:^(void){
         [__managedObjectContext
         mergeChangesFromContextDidSaveNotification:notification];
         }];
     }
    
    


3.3 对Core Data进行同步

  1. 确认iCloud是否可用
  2. 设置iCloud对持久化存储进行同步

     #define UBIQUITY_CONTAINER_IDENTIFIER @"[TEAM_ID].com.mycompany.myapp"
     #define UBIQUITY_CONTENT_NAME_KEY @"com.mycompany.myapp.CoreData"
    
      // Set up persistent Store Coordinator
     __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    
     // 1.设置数据库文件URL
     NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite",ManagedObjectModelFileName]];
     NSDictionary *options = nil;
    
     // 2.设置iCloud选项字典 
     if(iCloudSyncIfAvailable && _iCloudAvailable){
     [[NSBundle mainBundle] bundleIdentifier];
     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSURL *contentURL = [fileManager URLForUbiquityContainerIdentifier:UBIQUITY_CONTAINER_IDENTIFIER];
    
     NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                              UBIQUITY_CONTENT_NAME_KEY,
                              NSPersistentStoreUbiquitousContentNameKey,
                              contentURL,
                              NSPersistentStoreUbiquitousContentURLKey, 
                              nil];
    
     }
         else if(!_iCloudAvailable){
         NSLog(@"Attempted to set up iCloud Core Data Stack, but iCloud is unvailable");
     }
    
     // 3.创建持久化存储Add the persistent store to the persistent store coordinator
     NSError *error = nil;
     if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                                 configuration:nil 
                                                           URL:storeURL //1.数据库文件URL
                                                       options:options //2.iCloud选项字典 
                                                         error:&error]){
         // Handle the error
         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
         abort();
     }  
    
    
  3. Core Data持久化存储的变更通知
    代码见上,不同之处:

    • 注册通知的key为NSUbiquitousKeyValueStoreDidChangeExternallyNotification
    • 处理通知使用mergeChangesFromContextDidSaveNotification
    • 注意这里执行需要用performBlock因为:
      1) 通知在main queue之外出发但是执行需要在 main queue
      2) 创建NSManagedObjectContext的时候用的是NSMainQueueConcurrencyType所以它执行的代码都在main queue.

            NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] 
                                   initWithConcurrencyType:NSMainQueueConcurrencyType];
      
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值