0

私はiOS開発とCoreDataも初めてです。アプリの UITableViewcontroller にデータを表示するために .Net WCF サービスを呼び出しています。このデータを CoreData に保存しています。サーバーに新しいレコードを追加すると、それが UITableViewController に表示され、CoreData に保存されます。しかし、これは起こりません。シミュレーターで「コンテンツと設定のリセット」を実行してから、アプリケーションを実行する必要があります。また。これを行うと、アプリはサービスからの最新のレコードを表示します。また、CoreData に新しいレコードを保存します。wcf サービスと対話するために SUDZC を使用しています。サービスを呼び出し、データを UITableViewController に表示し、それをCoreData は次のようになります。

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

    [my_table setDataSource:self];
    [my_table setDelegate:self];

     EDViPadDocSyncService *service = [[EDViPadDocSyncService alloc]init];
     [service getAllCategories:self action:@selector(handleGetAllCategories:)];

}

-(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;

    self.myData = [[NSMutableArray array] init];//array for storing 'category name'
    self.catId = [[NSMutableArray array]init];//array for storing 'category ID'    

    self.myData=[self getCategories];

    /*store data in Core Data - START*/
    NSMutableArray *coreDataCategoryarray = [[NSMutableArray alloc]init];
    NSManagedObjectContext *context = [self managedObjectContext];
    Categories *newCategory;//this is the CoreData 'Category' object
    for(int j=0;j<[result count];j++)
    {
        EDVCategory *edvCat = [[EDVCategory alloc]init];//this is the SUDZC 'Category' object
        edvCat = [result objectAtIndex:j];

        if ([self.catId count]>0) {
                for (int i=0; i<[self.catId count]; i++) {

                    if ([edvCat categoryId] == [[self.catId objectAtIndex:i] integerValue]) {
                        checkFlag=TRUE;
                    }
                }
        }
        if (checkFlag == FALSE) {
            newCategory = [NSEntityDescription insertNewObjectForEntityForName:@"Categories" inManagedObjectContext:context];
            [newCategory setCategoryId:[NSNumber numberWithInt:[edvCat categoryId]]];
            [newCategory setCategoryName:edvCat.categoryName];
            [newCategory setDocCount:[NSNumber numberWithInt:[edvCat docCount]]];
            [newCategory setCategoryType:[NSNumber numberWithShort:[edvCat categoryType]]];
            [newCategory setSubCategoryId:[NSNumber numberWithInt:[edvCat subCategoryId]]];
            [coreDataCategoryarray addObject:newCategory];
        }
    }
    /*store data in Core Data - END*/

    NSError *error = nil;
    if (![context save:&error])
    {
        [coreDataCategoryarray release];
    }
    else
    {
        //return [coreDataCategoryarray autorelease];
        [coreDataCategoryarray autorelease];
    }
    self.myData=[self getCategories];
    [my_table reloadData]; 
}

-(NSMutableArray *)getCategories
{
    NSFetchRequest  *request = [[[NSFetchRequest alloc] init]autorelease];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Categories" inManagedObjectContext:__managedObjectContext];

    NSSortDescriptor *sortByName = [[[NSSortDescriptor alloc] initWithKey:@"categoryId" ascending:YES] autorelease];
    [request setSortDescriptors:[NSArray arrayWithObject:sortByName]];

    [request setEntity:entity];
    entity = nil;

    NSError *error = nil;
    NSMutableArray *fetchResults = [[__managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    [request setReturnsObjectsAsFaults:NO];

    NSManagedObject *aTabrss;
    NSMutableArray *arForGetCategory=[[NSMutableArray alloc]init];

    for (aTabrss in fetchResults){
        [arForGetCategory addObject:[aTabrss valueForKey:@"categoryName"]];
        [self.catId addObject:[aTabrss valueForKey:@"categoryId"]];
    }
    return (arForGetCategory);
}

サービスからの最新データを反映し、同時に CoreData(sqlite) に保存するには、コードにどのような変更を加える必要がありますか?

4

1 に答える 1

0

本当に必要なクラスは NSUserDefaults のようです:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html

于 2012-09-20T03:15:17.433 に答える