2012-09-20 33 views
0

我是iOS開發和CoreData的新手。我正在調用.Net WCF服務來顯示我的應用程序中的UITableViewcontroller中的數據。我將這些數據保存在CoreData中。當我在服務器上添加一條新記錄時,我希望它顯示在UITableViewController中,並保存在CoreData.But中,但這不會發生。我必須在Simulator上執行「Reset Contents and Settings」,然後運行應用程序再次。當我這樣做時,應用程序顯示服務中的最新記錄。它還保存了CoreData中的新記錄。我使用SUDZC與wcf服務進行交互。調用服務的代碼,在UITableViewController中顯示數據並將其保存到CoreData看起來是這樣的:從CoreData的WCF服務保存記錄

- (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(源碼)?

+0

這似乎遠遠複雜得多,我會想象它需要。這可能是問題所在。我的建議是查看「NSFetchedResultsController」,這樣至少可以讓更少的託管對象數組通過。這是'NSFetchedResultsController'上的[tutorial](http://www.raywenderlich.com/999/core-data-tutorial-how-to-use-nsfetchedresultscontroller),可能會有所幫助。 – FluffulousChimp

+0

@alanduncan: - 謝謝你的幫助。我使用的是NSFetchRequest.How與NSFetchedResultsController有什麼不同嗎?它們不是用來從數據庫中獲取數據的嗎?正如我所說的,我對CoreData和iOS開發非常陌生,很難理解它們之間的區別。 – user1550951

回答