简体   繁体   中英

Save data to core data - Swift

When I save data to Core Data , duplicate data aren't save in one row. When I save new data , one new row appears. That's happen error ( not exact data result ) display. How to handle Duplicate data in Core Data.

For your scenario, you need to add one unique column to uniquely identify one row like primary key, ignore if you have already.

Whenever you're going to insert a tuple in Core Data Entity, don't use

Obj C :

[NSEntityDescription insertNewObjectForEntityForName:@"Document" inManagedObjectContext:context];

Swift :

NSEntityDescription.insertNewObjectForEntityForName("Document", inManagedObjectContext:context) 

This will create a new tuple everytime.

Instead,

Write a method to check the data you are going to insert is already available or not.

If available, return that object of the record. So that, you can just update the values for the record rather than creating a new record for the data.

For Example,

Here I'm using PortionInbox as an Entity and storing unique value to tuple as TransID.

  1. First checking for the data already exists or not..

     PortionInbox *portionInbox = [self checkAvailabilityWithTransId:portionDict[@"TransID"] context:context]; 

    if (portionInbox == nil) { portionInbox = [NSEntityDescription insertNewObjectForEntityForName:@"PortionInbox" inManagedObjectContext:context]; }

  2. If already exists, get the Entity object to update the values that you have. otherwise, create a new entry for new data.

  3. Write some predicate to retrieve that particular entry from portionInbox Entity.

as following.. I've written in Obj C. Just change it to Swift.

-(PortionInbox *)checkAvailabilityWithTransId:(NSString *)transId context:(NSManagedObjectContext *)context{

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"PortionInbox" inManagedObjectContext:context];

[fetchRequest setEntity:entity];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"transID contains[cd] %@", transId];

[fetchRequest setReturnsObjectsAsFaults:NO];

[fetchRequest setPredicate:pred];

NSError* error;

NSArray *fetchedRecords = [context executeFetchRequest:fetchRequest error:&error];

if (fetchedRecords.count>0)
{
    return [fetchedRecords objectAtIndex:0];
}
else
{
    return nil;
}}

Hope it helps..

To save some data into CoreData you should use this example Code swift 3.0

let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext

    let user = NSEntityDescription.insertNewObject(forEntityName: "Example", into: context)
    user.setValue("GOODDUDE", forKey: "name")

    appDelegate.saveContext()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM