1

私のアプリは、デバイスが WiFi に接続されているときに .Net WCF サービスからデータを取得する必要があります。サーバーに新しい行が追加された場合は、その CoreData データベースに追加する必要があります。ローカル オブジェクトとリモート オブジェクトを比較するために NSDictionary を使用しています。コードは次のとおりです。

-(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 に割り当てることができないことです。(私によると) 新しいオブジェクトの値を管理対象オブジェクトに保存する必要があるコードにコメントしました。誰かがこれを達成するのを手伝ってもらえますか?

ありがとう

4

1 に答える 1

1

これは、最近のプロジェクトで行った保存の例です。私はラッパーに何かを持っているので、管理対象オブジェクトをフェッチして保存することは、私の側では少し奇妙に見えます。本当に私が見る唯一の大きな違いは、救うという行為です。新しいNSManagedObjectをコードの他の場所に保存していますか?

dict = (NSDictionary*)data;
        @try {
            if (dict) {
                CaretakerInfo* info = [GenericDataService makeObjectWithEntityName:NSStringFromClass([CaretakerInfo class])];
                [info setName:[dict valueForKey:@"name"]];
                [info setImageURL:[dict valueForKey:@"photo"]];
                [info setCaretakerID:[dict valueForKey:@"id"]];
                [GenericDataService save];
            }
            else {
                theError = [Error createErrorMessage:@"No Data" Code:-42];
            }
        }
        @catch (NSException *exception) {
            //return an error if an exception
            theError = [Error createErrorMessage:@"Exception Thrown While Parsing" Code:-42];
        }

そうでない場合は、次のようになります...

     NSError *error = nil;
     [context save:&error];

役立つデータ(エラー/警告/ログメッセージ)を抽出または割り当てるときに何が起こっているかについての情報がもうある場合。

于 2012-09-26T05:50:13.893 に答える