弹出视图控制器时Coredata无法正常工作

时间:2013-03-18 17:21:57

标签: iphone core-data uinavigationcontroller

我有2个viewcontrollers(比如A& B)...我现在有一些记录在A中我想编辑核心数据中的数据....为此我将导航控制器推到B ...那里我编辑核心数据中的数据.. Itz工作正常..这是我用来编辑的代码..

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

1 个答案:

答案 0 :(得分:0)

viewWillAppear未被调用,因为父视图始终保留在视图层次结构中。相反,实施navigationController:willShowViewController:animated:请参阅此问题:iOS 5 viewWillAppear not being called when popping a NavigationController