iOS 数据缓存

如何利用Xcode查看真机归档的数据

  • 第一步,在Xcode的window目录下,选择Devices,以下图

图片

  • 第二步, 点击Devices,截图以下

图片

  • 第三步, 选择本身的那个APP

图片

  • 第四步, 操做以下图

图片

  • 第五步, 按照图中选中操做便可

图片

  • 第六步, 下载下来的缓存数据

输入图片说明

  • 第七步,右击,单击显示包内容

输入图片说明

  • 第八步,包中显示的内容

输入图片说明

  • 第九步, 找到咱们缓存的模型数据库

输入图片说明

  • 第十步,用相应的数据库打开

输入图片说明

模拟器与真机的缓存路径

  • 一   模拟器缓存路径
NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:[NSString stringWithFormat:@"%zi%@.db",[PassFeedAppDelegate sharedInstance].authenticatedMember.uid,key]];
   BOOL ret = [NSKeyedArchiver archiveRootObject:modelArray toFile:file];
   if (ret) {
       YS_DBG(@"模拟器上-----TACityModel模型数据归档成功");
   }else{
       YS_DBG(@"模拟器上-----TACityModel模型数据归档失败");
   }
  • 二   真机缓存路径
NSString *Path = NSHomeDirectory();
    //注意,在真机归档时,请在拼接路径时加上Documents/,指定写入到Documents文件夹下,不然提示归档失败
    NSString *totalPath=[Path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%zi%@.db",[PassFeedAppDelegate sharedInstance].authenticatedMember.uid,key]];
    BOOL ret = [NSKeyedArchiver archiveRootObject:modelArray toFile:totalPath];
    if (ret) {
        YS_DBG(@"真机上-----TACityModel模型数据归档成功");
    }else{
        YS_DBG(@"真机上-----TACityModel模型数据归档失败");
    }
  • 三 为了方便真机和模拟器均可以使用,能够使用以下的宏
#if TARGET_IPHONE_SIMULATOR
    
  
#elif TARGET_OS_IPHONE
    

#endif
  • 四 综合以上四点,写成一个归档的方法
#pragma mark -  根据指定的key,归档指定的模型数组

+ (void)archiveOTAContryModelArray:(NSMutableArray *)modelArray key:(NSString *)key
{
#if TARGET_IPHONE_SIMULATOR
    
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:[NSString stringWithFormat:@"%zi%@.db",[PassFeedAppDelegate sharedInstance].authenticatedMember.uid,key]];
    BOOL ret = [NSKeyedArchiver archiveRootObject:modelArray toFile:file];
    if (ret) {
        YS_DBG(@"模拟器上-----TACityModel模型数据归档成功");
    }else{
        YS_DBG(@"模拟器上-----TACityModel模型数据归档失败");
    }

#elif TARGET_OS_IPHONE
    
    NSString *Path = NSHomeDirectory();
    //注意,在真机归档时,请在拼接路径时加上Documents/,指定写入到Documents文件夹下,不然提示归档失败
    NSString *totalPath=[Path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%zi%@.db",[PassFeedAppDelegate sharedInstance].authenticatedMember.uid,key]];
    BOOL ret = [NSKeyedArchiver archiveRootObject:modelArray toFile:totalPath];
    if (ret) {
        YS_DBG(@"真机上-----TACityModel模型数据归档成功");
    }else{
        YS_DBG(@"真机上-----TACityModel模型数据归档失败");
    }
#endif
}

截图以下数据库

输入图片说明

  • 五 既然有了归档的方法,那确定有解归档的方法,以下
#pragma mark -  根据指定的key,解归档指定的模型数组

+ (NSMutableArray *)unArchiveOTAContryModelArrayWithkey:(NSString *)key
{
#if TARGET_IPHONE_SIMULATOR
    
    NSString *file        = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:[NSString stringWithFormat:@"%zi%@.db",[PassFeedAppDelegate sharedInstance].authenticatedMember.uid,key]];
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
    return array;
    
#elif TARGET_OS_IPHONE

    NSString *Path        = NSHomeDirectory();
    //注意,在真机归档时,请在拼接路径时加上Documents/,指定写入到Documents文件夹下,不然提示归档失败
    NSString *totalPath=[Path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%zi%@.db",[PassFeedAppDelegate sharedInstance].authenticatedMember.uid,key]];
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:totalPath];
    return array;
#endif
}

截图以下数组

输入图片说明

  • 六 归档和解归档的方法都有了,那么确定还会有删除这个模型数据库的方法
#pragma mark -  根据指定的key,删除指定的模型数组
+ (void)deleteOTAContryModelArrayWithkey:(NSString *)key
{
#if TARGET_IPHONE_SIMULATOR
    
    NSString *file        = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:[NSString stringWithFormat:@"%zi%@.db",[PassFeedAppDelegate sharedInstance].authenticatedMember.uid,key]];
    [MessageUtil_ deleteFileWithPath:file];
    
#elif TARGET_OS_IPHONE
    
    NSString *Path = NSHomeDirectory();
    //注意,在真机归档时,请在拼接路径时加上Documents/,指定写入到Documents文件夹下,不然提示归档失败(模拟器上路径前面不须要Documents/,真机才要)
    NSString *totalPath=[Path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%zi%@.db",[PassFeedAppDelegate sharedInstance].authenticatedMember.uid,key]];
    [MessageUtil_ deleteFileWithPath:totalPath];
#endif
}

截图以下缓存

输入图片说明

  • 七 好了,早下班了,今天的问题也都解决了,该回家啰!欢迎大批评指正!再见!
相关文章
相关标签/搜索