1
當設備連接到WiFi時,我的應用程序需要從.Net WCF服務獲取數據。如果服務器上添加了新行,則應將其添加到其CoreData數據庫。我正在使用NSDictionary比較本地對象與遠程對象。該代碼是:將值從NSDictionary分配給NSManagedObject
-(void)handleGetAllCategories:(id)value
{
if([value isKindOfClass:[NSError class]])
{
NSLog(@"This is an error %@",value);
return;
}
if([value isKindOfClass:[SoapFault class]])
{
NSLog(@"this is a soap fault %@",value);
return;
}
NSMutableArray *result = (NSMutableArray*)value;
NSMutableArray *remoteObj = [[NSMutableArray alloc]init];
for (int i = 0; i < [result count]; i++)
{
EDVCategory *catObj = [[EDVCategory alloc]init];
catObj = [result objectAtIndex:i];
[remoteObj addObject:catObj];
}
NSArray *remoteIDs = [remoteObj valueForKey:@"categoryId"];
NSFetchRequest *request = [[[NSFetchRequest alloc] init]autorelease];
request.predicate = [NSPredicate predicateWithFormat:@"categoryId IN %@", remoteIDs];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Categories" inManagedObjectContext:__managedObjectContext];
[request setEntity:entity];
NSMutableArray *results = [[NSMutableArray alloc]initWithArray:[__managedObjectContext executeFetchRequest:request error:nil]];
NSArray *existingIDs = [results valueForKey:@"categoryId"];
NSDictionary *existingObjects = [NSDictionary dictionaryWithObjects:results forKeys:existingIDs];
for (NSDictionary *remoteObjectDic in remoteObj)
{
Categories *existingObject = [existingObjects objectForKey:[remoteObjectDic valueForKey:@"categoryId"]];
if (existingObject)
{
NSLog(@"object exists");
}
else
{
NSLog(@"create new local object");
// Categories *newCategory;
// newCategory = [NSEntityDescription insertNewObjectForEntityForName:@"Categories" inManagedObjectContext:__managedObjectContext];
// [newCategory setCategoryId:[NSNumber numberWithInt:[[remoteObjectDic objectForKey:@"categoryId"]intValue]]];
// [newCategory setCategoryName:[remoteObjectDic objectForKey:@"categoryName"]];
// [newCategory setDocCount:[NSNumber numberWithInt:[[remoteObjectDic objectForKey:@"docCount"]intValue]]];
// [newCategory setCategoryType:[NSNumber numberWithInt:[[remoteObjectDic objectForKey:@"categoryType"]intValue]]];
// [newCategory setSubCategoryId:[NSNumber numberWithInt:[[remoteObjectDic objectForKey:@"subCategoryId"]intValue]]];
// [__managedObjectContext insertObject:newCategory];
}
}
[my_table reloadData];
}
的問題是,我不能夠從遠程對象中提取值,並將其分配給NSManagedObject.I有評論它(據我)的代碼應該保存在新的價值觀對象到託管對象。有人可以幫我實現嗎?
謝謝
: - 謝謝,它的功能就像一個魅力! – user1550951
現在,如果我要從CoreData中刪除一個對象(bcoz它在服務器上被刪除),我該如何做? – user1550951
[context deleteObject:object];不要忘記保存後:) – Kibitz503