2013-03-18 16 views
0

我有2個viewcontrollers(說A & B)...我有一些記錄在A現在我想編輯核心數據中的數據....爲此,我推動導航控制器到B。有我在覈心數據編輯數據..伊茨工作正常..這是我使用的編輯代碼..Coredata不工作時彈出視圖控制器

NSManagedObjectContext *managedContext = [self managedObjectContext]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CamImage" inManagedObjectContext:managedObjectContext]; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entity]; 

    // Set the predicate -- much like a WHERE statement in a SQL database 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", xapp.patientName]; 
    [request setPredicate:predicate]; 

    // Set the sorting -- mandatory, even if you're fetching a single record/object 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
    [request setSortDescriptors:sortDescriptors]; 
    sortDescriptors = nil; 
    NSError *error; 
    // Request the data -- NOTE, this assumes only one match, that 
    // yourIdentifyingQualifier is unique. It just grabs the first object in the array. 
    CamImage *event = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0]; 

    [event setMedicineImage:butImage]; 
    [event setMedicineName:text.text]; 
    [event setQuantity:[jst objectAtIndex:0]]; 
    [event setUnit:[jst objectAtIndex:1]]; 
    [event setMeal:meal]; 
    request = nil; 

    NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request1 
                        error:&error] mutableCopy]; 
    if (mutableFetchResults == nil) { 
     // Handle the error. 
    } 

    // Set self's events array to the mutable array, then clean up. 
    [self setCamImage:mutableFetchResults]; 
    NSLog(@"AddMedicineArray:%@",camImage); 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

在上面的代碼中,我編輯的數據,並再次獲取它。 。我可以找到正確編輯的數據..編輯後,我彈出到B ..在viewwillappear方法我再次獲取數據..但我找到舊的記錄只...一旦我再次運行應用程序,我發現編輯的數據得到正確..這裏是代碼在viewwillappear

-(void)viewWillAppear:(BOOL)animated{ 

    camImage=Nil; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"CamImage" inManagedObjectContext:self.managedObjectContext]; 
    [request setReturnsObjectsAsFaults:NO]; 
    [request setEntity:entity]; 

    NSError *error = nil; 
    NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
    if (mutableFetchResults == nil) { 
     // Handle the error. 
    } 

    // Set self's events array to the mutable array, then clean up. 
    [self setCamImage:mutableFetchResults]; 

回答

相關問題