Save a lot of code by using NSClassFromString in Objective C
来源:互联网 发布:淘宝关键词上首页技术 编辑:程序博客网 时间:2024/06/01 09:28
Recently, I am implementing database synchronize feature by using SQL sync framework 2.1. In the iOS client, I need save each row of data (entity) into its corresponding table.
From the metadata of the response, I can see the entity table name and its columns' value and name. Usually, I need write method for each table to save the entity into the table, like below piece of code: Status and Priority are the tables' name.
// populates the StatusEntity store with the values from the json object dict
+ (void)populateStatus: (id)dict withMetadata:(id)metadata withContext:(NSManagedObjectContext*) context;
{
bool isTombstone = [Utils isDeleted:metadata];
StatusEntity *status = nil;
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"Status" inManagedObjectContext:context]];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"ID = %@", [dict valueForKey:@"ID"]]];
NSArray *results = [context executeFetchRequest:fetch error:nil];
if (results.count == 1)
{
status = [results objectAtIndex:0];
}
if (status == nil && !isTombstone)
{
// insert new status
status = [NSEntityDescription insertNewObjectForEntityForName:@"Status" inManagedObjectContext:context];
}
// if its deleted and we have not seen it ignore it
// else delete it
if (isTombstone && status != nil)
{
[context deleteObject:status];
}
else if (!isTombstone)
{
status.ID = [dict valueForKey:@"ID"];
status.statusName = [dict valueForKey:@"Name"];
}
}
// populates the PriorityEntity store with the values from the json object dict
+ (void)populatePriority: (id)dict withMetadata:(id)metadata withContext:(NSManagedObjectContext*) context;
{
bool isTombstone = [Utils isDeleted:metadata];
PriorityEntity *priority = nil;
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"Priority" inManagedObjectContext:context]];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"ID = %@", [dict valueForKey:@"ID"]]];
NSArray *results = [context executeFetchRequest:fetch error:nil];
if (results.count == 1)
{
priority = [results objectAtIndex:0];
}
if (priority == nil && !isTombstone)
{
// insert new status
priority = [NSEntityDescription insertNewObjectForEntityForName:@"Priority" inManagedObjectContext:context];
}
// if its deleted and we have not seen it ignore it
// else delete it
if (isTombstone && priority != nil)
{
[context deleteObject:priority];
}
else if (!isTombstone)
{
priority.ID = [dict valueForKey:@"ID"];
priority.priorityName = [dict valueForKey:@"Name"];
}
}
Thanks NSClassFromString method, which saved me a lot of time and code to just use a method to handle all the entities automatically.
The most important methods are NSClassFromString and insertNewObjectForEntityForName:
Class cls = NSClassFromString(tableName);
if (cls != Nil)
{
// if the name is a class object, then we can call the class method directly.
newObject = [cls findFirstWithPredicate:predicate];
}
and:
if (NSClassFromString(name)) {
// Based on the entity name, we can generate the entity object with below method directly. It is very helpful.
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:context];
}
Here is the full code on my implementation, some methods are my wrapped internal method for the CoreData, which are not exposed here. But it will be very help for the reader to see how did I insert the entity :
// populates the ListEntity store with the values from the json object dict
+ (void)populateEntity: (id)dict withMetadata:(id)metadata withContext:(NSManagedObjectContext*) context {
bool isTombstone = [Utils isDeleted:metadata];
NSString *uri = [metadata valueForKey:@"uri"];
NSMutableDictionary *pKeys = [NSMutableDictionary dictionary];
NSString *tableName = nil;
NSArray *sub = [uri componentsSeparatedByString:@".svc/"];
if ([sub count] == 2) {
uri = [sub objectAtIndex:1];
NSArray *subStrings = [uri componentsSeparatedByString:@"("];
if ([subStrings count] == 2) {
tableName = [subStrings objectAtIndex:0];
// (Robert #) in Heat3, the table name is capitalized. So need special convert it here.
tableName = [NSString stringWithFormat:@"%@%@",[tableName substringToIndex:1].uppercaseString, [tableName substringFromIndex:1]];
NSString *keys = [[subStrings objectAtIndex:1] stringByReplacingOccurrencesOfString:@")" withString:@""];
NSArray *subKeys = [keys componentsSeparatedByString:@","];
for (NSString *key in subKeys) {
NSArray *keyPair = [key componentsSeparatedByString:@"="];
if ([keyPair count] == 2) {
[pKeys setObject:[keyPair objectAtIndex:1] forKey: [keyPair objectAtIndex:0]];
}
}
}
} else {
NSLog(@"Something is wrong, please check.");
}
if (nil != tableName && [pKeys allKeys] > 0)
{
NSManagedObject *newObject = nil;
// measure = [TgMeasure findFirstByAttribute:@"MeasureID" withValue:[dict valueForKey:@"MeasureID"]];
if (NSClassFromString(tableName)) {
NSString *string = [NSString string];
for (NSString *key in [pKeys allKeys]) {
if ([string length] == 0) {
string = [string stringByAppendingFormat:@"%@=%@", key, [pKeys objectForKey:key]];
} else {
string = [string stringByAppendingFormat:@" and %@=%@", key, [pKeys objectForKey:key]];
}
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:string];
Class cls = NSClassFromString(tableName);
if (cls != Nil)
{
newObject = [cls findFirstWithPredicate:predicate];
}
}
// if its deleted and we have not seen it ignore it
// else delete it
if (isTombstone && newObject != nil)
{
[context deleteObject:newObject];
}
else if (!isTombstone)
{
[NSManagedObject createManagedObjectFromDictionary:dict inContext:CONTEXT_FOR_CURRENT_THREAD withName:tableName];
SAVE_COREDATA_CONTEXT;
}
}
}
+ (NSManagedObject *)createManagedObjectFromDictionary:(NSDictionary *)dictionary inContext:(NSManagedObjectContext *)context withName:(NSString *)name
{
if (NSClassFromString(name)) {
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:context];
NSMutableDictionary *tmpDict = [NSMutableDictionary new];
for(NSString *aKey in dictionary){
id object = [dictionary valueForKey:aKey];
NSString * newKey = [aKey lowercaseString];
[tmpDict setValue:object forKey:newKey];
}
[newObject fromDictionary:tmpDict];
return newObject;
} else {
return nil;
}
}
- Save a lot of code by using NSClassFromString in Objective C
- Programming in C has a lot of fun!
- A lot of things
- A Lot of Joy
- Using C code in symbian
- SQL: How to get a series of dates in sql using connect by
- Oracle HANG by the MMAN spinning consuming a lot of CPU
- system play - qemu with a lot of system imgs (maybe I'll develop one by myself)
- Using Properties in Objective-C Tutorial 边看边记
- File extensions of Objective-C code
- Packet Sniffer Code in C using sockets
- Using CSharp (C#) code in Powershell scripts
- A Problem of JAXB (Source: "An analysis of interface specification in XML using Design by Contract ", Author: Yichong Zhou)
- How to save content/text of a web page by forcing save-as option
- Generation of a LOT number for an Inventory item via API in R12
- Why do a lot of #defines in the kernel use do { ... } while(0)?
- Why do a lot of #defines in the kernel use do { ... } while(0)?
- Ubuntu typing 'php' in terminal shows a lot of errors 终端terminal 输入php 错误
- 第二章 IoC Bean的作用域
- "ORA-01102 cannot mount database in EXCLUSIVE mode",官方文档:
- 利用形态学检测边缘和角点
- centOS开放http端口
- cocos2d-x Android环境下用NDK7以上版本编译(免Cygwin)
- Save a lot of code by using NSClassFromString in Objective C
- WikiOI 1063 合并果子
- HBase如何合理设置客户端Write Buffer
- 让Delphi XE2程序支持UAC
- AIX的页面空间pagingSpace
- C#的方法参数--params、ref、out
- 面试题1 赋值运算符函数
- C使用FILE指针文件操作
- 在PDF中嵌入Code128条形码字库