how to update core data entered values

前端 未结 2 1545
南方客
南方客 2021-01-05 11:18
[newVehicle setValue: _txtFieldVehicleNumber.text forKey:@\"number\"];

[newVehicle setValue: lblFuelType.text forKey:@\"fueltype\"];

[newVehicle setValue: lblFuelU         


        
相关标签:
2条回答
  • 2021-01-05 11:28

    Get an entity from core data:

    VVdAppDelegate *delegate = (VVdAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext];
    NSEntityDescription *entityDescriptionDelegate = [NSEntityDescription entityForName:@"entityName" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *requestData = [[NSFetchRequest alloc] init];
    [requestData setEntity:entityDescriptionDelegate];
    NSError *error;
    

    If there are more than an NSManagedObject

    NSArray *objectsArray = [managedObjectContext executeFetchRequest:requestData error:&error];
    
    for (NSManagedObject *object in objectsArray) {
    
            // Here update values of every object
    
    }
    
    // And save values to core data
    [managedObjectContext save:&error];
    
    0 讨论(0)
  • 2021-01-05 11:48

    you can do the following (approximate code. Implement error handling and check syntax).

    NSFetchRequest *fetchRequest=[NSFetchRequest fetchRequestWithEntityName:@"Vehicle"];
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"vehicle_id==%@",vehicle_id]; // If required to fetch specific vehicle
    fetchRequest.predicate=predicate;
    Vehicle *newVehicle=[[self.managedObjectContext executeFetchRequest:fetchRequest error:nil] lastObject];
    
    [newVehicle setValue: _txtFieldVehicleNumber.text forKey:@"number"];
    
    [newVehicle setValue: lblFuelType.text forKey:@"fueltype"];
    
    [newVehicle setValue: lblFuelUnit.text forKey:@"fuelunit"];
    
    [newVehicle setValue: lblDistanceUnit.text forKey:@"distanceunit"];
    
    [self.managedObjectContext save:nil]
    
    0 讨论(0)
提交回复
热议问题