美文网首页
BaseDataModel示例(3)-基于BaseDataMod

BaseDataModel示例(3)-基于BaseDataMod

作者: fredericoyang | 来源:发表于2021-04-29 18:58 被阅读0次

    基于BaseDataModel的模型自动归档解档,支持嵌入式基于BaseDataModel的模型 Model based on BaseDataModel archive and unarchive automatically, whether it embed model based on BaseDataModel or not

    要实现这个需求,需要满足两个条件:
    1、要支持归档解归档,要实现 NSCoding 协议里面的方法。
    2、要支持自动归档解归档,需要自动完成自定义模型及其下包含的所有自定义模型的 NSCoding 协议方法,为了支持 secureCoding,还必须要让自定义模型及其下包含的所有自定义模型的 +supportsSecureCoding都返回 YES。

    我们提供了两个 NSObject的类 Class和 Coding,以此实现了这个需求。BaseDataModel 已包含了这些实现,自然也继承了这一特性。

    尽情享用吧!


    For English here if you do not understand Chinese:

    To implement this requirement, two conditions need to be met:

    1. To support archive unarchive, to implement the methods in the NSCoding protocol.

    2. To support automatic archiving and unarchiving, it is necessary to automatically complete the NSCoding protocol methods of the custom model and all the other custom models contained. In order to support secureCoding, You must also make sure the +supportsSecureCoding method of the custom model and all the other custom models contained return YES.

    We provide two NSObject categories, the Class and Coding, to implement the requirement. BaseDataModel also inherits this feature by referring to this implmentation.

    Enjoy the follow time!

    @interface ContactInfoModel : BaseDataModel
    
    @property (copy, nonatomic, nullable) NSString<Optional> *address;
    @property (copy, nonatomic, nullable) NSString<Optional> *mobilephone;
    @property (copy, nonatomic, nullable) NSString<Optional> *email;
    
    @end
    
    @protocol ContactInfoModel <NSObject>
    
    @end
    
    @interface User : BaseDataModel
    
    @property (copy, nonatomic, nullable) NSString<Optional> *nick;
    @property (strong, nonatomic, nullable) NSNumber<Optional> *age;
    @property (strong, nonatomic, nullable) ContactInfoModel<Optional> *contactInfo;
    
    @end
    
    
    @implementation ContactInfoModel
    
    @end
    
    @implementation User
    
    +(JSONKeyMapper*)keyMapper
    {
        return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
            @"dataID": @"ID",      // dataID用作ID
          @"dataName": @"name"   // dataName用作name
        }];
    }
    
    @end
    
    
    // Test
    NSArray *array = @[@{@"ID": @57,
                       @"name": [NSNull null],
                       @"nick": @"Tony",
                        @"age": [NSNull null],
                @"contactInfo": @{@"address": [NSNull null],
                              @"mobilephone": @"136xxxxxxxx",
                                    @"email": [NSNull null]},
                    @"offline": @true},
                       @{@"ID": @58,
                       @"name": [NSNull null],
                       @"nick": @"Jim",
                        @"age": [NSNull null],
                @"contactInfo": @{@"address": [NSNull null],
                              @"mobilephone": @"136xxxxxxxx",
                                    @"email": [NSNull null]},
                    @"offline": @true},
                       @{@"ID": @59,
                       @"name": [NSNull null],
                       @"nick": @"Sam",
                        @"age": [NSNull null],
                @"contactInfo": @{@"address": [NSNull null],
                              @"mobilephone": @"136xxxxxxxx",
                                    @"email": [NSNull null]},
                    @"offline": @true}];
    
    NSMutableArray *mArray = [NSMutableArray array];
    for (NSDictionary *dictionary in array) {
        NSError *error;
        User *model = [[User alloc] initWithDictionary:dictionary error:&error];
        if (model) {
            [mArray addObject:model];
        }
    }
    NSArray *searchPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = searchPath.firstObject;
    NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"test.archive"];
    
    //TODO: Archive and save models of User
    NSError *error;
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mArray requiringSecureCoding:YES error:&error];
    if (data) {
        if ([data writeToFile:filePath options:NSDataWritingAtomic error:&error]) {
            LOG_FORMAT(@"Archive success.");
        }
    }
    
    //TODO: Read and unarchive models of User
    data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
    NSArray *users_array = [NSKeyedUnarchiver unarchivedArrayOfObjectsOfClass:[User class] fromData:data error:&error];
    if (users_array) {
        LOG_FORMAT(@"Unarchive success. User models: %@", users_array);
    }
    

    相关

    • 详见极致框架官网<extreme.framework/BaseDataModel.h>的介绍。通过极致框架官网顶部的搜索功能搜索 BaseDataModel。

    许可

    • 本文采用 BY-NC-SA 许可协议。即:署名——转载请注明出处;非商业使用;相同方式传播——再分发的文章许可与原文相同。

    查看原文

    相关文章

      网友评论

          本文标题:BaseDataModel示例(3)-基于BaseDataMod

          本文链接:https://www.haomeiwen.com/subject/xpwwrltx.html