iOS项目拆分:数据本地持久化(1)

在这里本人将以往做过的项目一一做了总结,并且拆分成一个个细小的功能模块(这里不包括UI界面搭建),封装抽类成一个个独立的功能。会把涉及到的功能点和知识点做详细的注释,便于理解、学习、使用。

重要数据本地存储

重要数据这里所要说的是用户数据,包含用户相关信息,以便用户操作APP,前端和服务器的数据交互。 数据本地持久化(所谓的持久化,就是将数据保存到手机硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据)方式:
写入本地文件(plist文件)
NSUserDefaults(Preference偏好设置)
NSKeyedArchiver(归档)
coreData
sqlite3
FMDB(FMDB是S对qlite数据库的封装)

这里主要讲解用户信息数据写本地文件(plist文件)
1.创建单例,在单例里面将数据存入沙盒文件中

#import <Foundation/Foundation.h>
@class MyUserModel;

@interface MyUserManager : NSObject
// 用户信息
@property (nonatomic, strong) MyUserModel * activeUser;

/**
 *  单例
 *  @return 实例对象
 */
+ (instancetype)shareManager ;
/**
 *  沙盒保存用户信息
 *  @param responseObject 服务器返回的用户数据
 *  @param errorMessage   错误信息
 *  @return 是否保存成功
 */
- (BOOL)saveWithResponseObjectAsUserInfo:(NSDictionary *)responseObject withErrorMessage:(NSString *)errorMessage ;
@end
#import "MyUserManager.h"
#import "MyUserModel.h"

@implementation MyUserManager
/**
 *  单例
 *
 *  @return 实例对象
 */
static MyUserManager * defualt_shareMananger = nil;
+ (instancetype)shareManager {

    static dispatch_once_t onceToken;
    _dispatch_once(&onceToken, ^{

        if (defualt_shareMananger == nil) {
            defualt_shareMananger = [MyUserManager new];
        }
    });
    return defualt_shareMananger;
}

#pragma mark - 沙盒存储-写本地

/**
 *  沙盒保存用户信息
 *  @param responseObject 服务器返回的用户数据
 *  @param errorMessage   错误信息
 *  @return 是否保存成功
 */
- (BOOL)saveWithResponseObjectAsUserInfo:(NSDictionary *)responseObject withErrorMessage:(NSString *)errorMessage {

    if (!isNULL(responseObject)) {
        // 获取到存储路径
        NSString *filePath = [self documentFilePathWithUserInfo];
        // 用户信息转化
        MyUserModel * userInfo = [MyUserModel mj_objectWithKeyValues:responseObject];
        // 判断用户是否正常登陆
        if (userInfo.auth_token.length == 0 || userInfo.userID.length == 0) {

            if (errorMessage != NULL) {
                errorMessage = @"登录失败";
                return NO;
            }
        }
        // 将数据写入沙盒
        BOOL result = [responseObject writeToFile:filePath atomically:YES];
        if (result) {
            if (errorMessage != NULL) errorMessage = @"登录成功";
            return YES;

        }else {

            if (errorMessage != NULL)  errorMessage = @"登录失败";
            return NO;
        }
    }else {
        if (errorMessage != NULL)  errorMessage = @"登录失败";
        return NO;
    } 
}
/**
 *  沙盒路径
 *  @return
 */
- (NSString *)documentFilePathWithUserInfo {

    // 一、获取到当前的Document文件夹的路径
    // 1、NSSearchPathDirectory directory 第一个参数:我们要显示的文件夹是哪一个
    // 2、NSSearchPathDomainMask domainMask 第二个参数:是在哪个域(用户权限)下面
    // 3、BOOL expandTilde  第三个参数 :预留参数
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    // 将前面的路径格式和后面的普通的字符串格式链接在一起,并且以路径格式返回。
    NSString * filePath = [documentPath stringByAppendingPathComponent:@"userInfo"];
    return filePath;
}
/**
 *  当前用户
 *  @return activeUser
 */
- (MyUserModel *)activeUser {
    if (_activeUser != nil) {
        return _activeUser;
    }
    // 获取数据存储路径
    NSString *filePath = [self documentFilePathWithUserInfo];
    // 查询数据是否存在
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
    if (isExist) {
        // 取出数据
        NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
        if (dic) {
            MyUserModel * userModel = [MyUserModel mj_objectWithKeyValues:dic];
            if (userModel) {
                _activeUser = userModel;
            }
        }
        return _activeUser;
    }else {
        return nil;
    }
}
@end

2.在APP登录后,服务器会将对应的用户数据返回给你,这时需要调用沙盒存储方法,把用户数据存储到本地,以便用户的其他操作。
其他的持久化方式也会一一介绍其用法和用途,可以继续关注这个博客专栏。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值