CoreData與Mantle的結合使用案例
阿新 • • 發佈:2019-01-04
Mantle(https://github.com/Mantle/Mantle)是一個用於操作CoreData的封裝庫。這個庫的作用有兩個,一是方便的對包含json資料的NSDictionary物件進行解析,並且使用其初始化物件。二是可以方便的將物件儲存到CoreData中去。
先說第一個功能點。首先你的類需要繼承MTLModel,然後實現MTLJSONSerializing 這個協議。這個協議是用來定義如何處理傳入的NSDictionary物件的。
#import <Mantle.h>
@interface EquipmentData : MTLModel<MTLJSONSerializing>
@property (copy, nonatomic, readonly) NSString *equipmentName;
@property (copy, nonatomic, readonly) NSString *equipmentLevel;
@end
在m檔案中需要實現 + (NSDictionary *)JSONKeyPathsByPropertyKey; 這個方法是Required實現的。這個方法的作用是將NSDictionary中鍵與EquipmentData中的property一一對應,如果兩者名字相同,這無需註明。
例如:NSDictionary的資料是@{@“name”:@“武器一”,@“equipmentLevel”:@“等級一"}
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{@“equipmentName”:@“name”};//將Dictionary中“name”鍵值傳入到“equipmentName”中,
}
然後,當你想建立新的EquipmentData時候,只需要按下面程式碼的形式就可以實現了。
EquipmentData *data = [MTLJSONAdapter modelOfClass:[EquipmentData class] fromJSONDictionary:@{@“name”:@“武器一”,@“equipmentLevel”:@“等級一"} error:&error];
是不是簡化了很多的json操作,但是問題還有,並不是所有的型別json都支援的,比如URL,NSDate等等,還有NSArray型別的屬性儲存的是自定義物件如何轉化,在最後的demo專案中有具體的示範。
現在介紹第二個功能點,將mantle物件儲存到CoreData中。
為什麼不用CoreData呢,這是Mantle官方給出的解答:
Why Not Use Core Data?
Core Data solves certain problems very well. If you need to execute complex queries across your data, handle a huge object graph with lots of relationships, or support undo and redo, Core Data is an excellent fit.
It does, however, come with a couple of pain points:
• There's still a lot of boilerplate. Managed objects reduce some of the boilerplate seen above, but Core Data has plenty of its own. Correctly setting up a Core Data stack (with a persistent store and persistent store coordinator) and executing fetches can take many lines of code.
• It's hard to get right. Even experienced developers can make mistakes when using Core Data, and the framework is not forgiving.
If you're just trying to access some JSON objects, Core Data can be a lot of work for little gain.
Nonetheless, if you're using or want to use Core Data in your app already, Mantle can still be a convenient translation layer between the API and your managed model objects. 首先接受下我們CoreData的Model檔案:
EquipmentData模型
PlayerData模型
這兩個模型之間有一個一對多的關係,一個PlayerData物件有一多個equipments物件,一個EquipmentData屬於一個PlayerData。
在使用Mantle後,只需要在PlayerData.m檔案中實現:
#pragma mark - CoreData
+ (NSString *)managedObjectEntityName
{
return NSStringFromClass([self class]);
}
+ (NSDictionary *)managedObjectKeysByPropertyKey
{
return @{};
}
就可以直接儲存只含有基礎型別的CoreData物件了。
這個例子中還有一個equipments的一對多關係,再實現下面用於轉換的方法就可以了
+ (NSDictionary *)relationshipModelClassesByPropertyKey
{
return @{@"equipments":EquipmentData.class};
}
這裡只是介紹了簡單的Mantle用法,還有一些進階的方法需要大家在demo專案中自行理解了,這裡就不再多做介紹了
連結: http://pan.baidu.com/s/1c0cWpyg 密碼: trba
先說第一個功能點。首先你的類需要繼承MTLModel,然後實現MTLJSONSerializing 這個協議。這個協議是用來定義如何處理傳入的NSDictionary物件的。
#import <Mantle.h>
@interface EquipmentData : MTLModel<MTLJSONSerializing>
@property (copy, nonatomic, readonly) NSString *equipmentName;
@property (copy, nonatomic, readonly) NSString *equipmentLevel;
@end
在m檔案中需要實現 + (NSDictionary *)JSONKeyPathsByPropertyKey; 這個方法是Required實現的。這個方法的作用是將NSDictionary中鍵與EquipmentData中的property一一對應,如果兩者名字相同,這無需註明。
例如:NSDictionary的資料是@{@“name”:@“武器一”,@“equipmentLevel”:@“等級一"}
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{@“equipmentName”:@“name”};//將Dictionary中“name”鍵值傳入到“equipmentName”中,
}
然後,當你想建立新的EquipmentData時候,只需要按下面程式碼的形式就可以實現了。
EquipmentData *data = [MTLJSONAdapter modelOfClass:[EquipmentData class] fromJSONDictionary:@{@“name”:@“武器一”,@“equipmentLevel”:@“等級一"} error:&error];
是不是簡化了很多的json操作,但是問題還有,並不是所有的型別json都支援的,比如URL,NSDate等等,還有NSArray型別的屬性儲存的是自定義物件如何轉化,在最後的demo專案中有具體的示範。
現在介紹第二個功能點,將mantle物件儲存到CoreData中。
為什麼不用CoreData呢,這是Mantle官方給出的解答:
Why Not Use Core Data?
Core Data solves certain problems very well. If you need to execute complex queries across your data, handle a huge object graph with lots of relationships, or support undo and redo, Core Data is an excellent fit.
It does, however, come with a couple of pain points:
• There's still a lot of boilerplate. Managed objects reduce some of the boilerplate seen above, but Core Data has plenty of its own. Correctly setting up a Core Data stack (with a persistent store and persistent store coordinator) and executing fetches can take many lines of code.
• It's hard to get right. Even experienced developers can make mistakes when using Core Data, and the framework is not forgiving.
If you're just trying to access some JSON objects, Core Data can be a lot of work for little gain.
Nonetheless, if you're using or want to use Core Data in your app already, Mantle can still be a convenient translation layer between the API and your managed model objects. 首先接受下我們CoreData的Model檔案:
EquipmentData模型
PlayerData模型
這兩個模型之間有一個一對多的關係,一個PlayerData物件有一多個equipments物件,一個EquipmentData屬於一個PlayerData。
在使用Mantle後,只需要在PlayerData.m檔案中實現:
#pragma mark - CoreData
+ (NSString *)managedObjectEntityName
{
return NSStringFromClass([self class]);
}
+ (NSDictionary *)managedObjectKeysByPropertyKey
{
return @{};
}
就可以直接儲存只含有基礎型別的CoreData物件了。
這個例子中還有一個equipments的一對多關係,再實現下面用於轉換的方法就可以了
+ (NSDictionary *)relationshipModelClassesByPropertyKey
{
return @{@"equipments":EquipmentData.class};
}
這裡只是介紹了簡單的Mantle用法,還有一些進階的方法需要大家在demo專案中自行理解了,這裡就不再多做介紹了
連結: http://pan.baidu.com/s/1c0cWpyg 密碼: trba