Git Product home page Git Product logo

Comments (8)

gonzalezreal avatar gonzalezreal commented on August 22, 2024

This is something that could be implemented inside Groot, or you can do it in a category.

Here's what I would do:

- (void)updateAndDelete {
    NSArray *objects = [GRTJSONSerialization mergeObjectsForEntityName:@"Test"
                                                         fromJSONArray:JSONResponse
                                                inManagedObjectContext:myContext
                                                                 error:&error];

    // Fetch unmodified objects
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Test"];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"NOT (self IN %@)", objects];
    fetchRequest.includesPropertyValues = NO; // We just need the object IDs

    NSArray *objectsToDelete = [myContext executeFetchRequest:fetchRequest error:NULL];

    // Delete unmodified objects
    for (NSManagedObject *obj in objectsToDelete) {
        [myContext deleteObject:obj];
    }

    // Save...
}

from groot.

soulchild avatar soulchild commented on August 22, 2024

Nice, clear and concise. Thanks a lot! ;-)

from groot.

gonzalezreal avatar gonzalezreal commented on August 22, 2024

No problem. I will think about adding this functionality, but in the meantime you can just use that code.

from groot.

gonzalezreal avatar gonzalezreal commented on August 22, 2024

Reopening so that it's visible to everyone.

from groot.

soulchild avatar soulchild commented on August 22, 2024

I just realized that the code you provided only works on the top level entity. For instance, take the following JSON structure (mapped to Category and Item Core Data entities):

[
  {
    id: 1,
    title: "Category",
    items: [
      {
        id: 1,
        title: "Item #1"
      },
      {
        id: 2,
        title: "Item #2"
      }
    ]
  }
]

Now, when fetching all Category entities which have a too-many relationship with Item entities via items, and one of the items is removed from the JSON, it is not deleted from my Core Data store. The updateAndDelete method you suggested only works on top-level entities (Category in this example).

Any ideas on how to tackle this problem are greatly appreciated!

from groot.

gonzalezreal avatar gonzalezreal commented on August 22, 2024

You can setup a delete rule in your relationship to cascade deletes. Check the Core Data Inspector with your items relationship selected.

from groot.

gonzalezreal avatar gonzalezreal commented on August 22, 2024

Sorry, I misread your question. The delete rule has no use here. In that case, the only viable solution would be to have a deleted field in your JSON that is mapped to your model. After processing the JSON you could perform a fetch request for all deleted items and remove them from the database.

from groot.

scriptease avatar scriptease commented on August 22, 2024

Thanks i use the folllowing swift version now:

func remainingObjectsFromJSONArray<T: NSManagedObject>(array: JSONArray, inContext context: NSManagedObjectContext) throws -> [T] {
    let objects: [T] = try objectsFromJSONArray(array, inContext:context)

    let entity = T.entityInManagedObjectContext(context)

    let fetchRequest = NSFetchRequest(entityName: entity.name!);
    fetchRequest.predicate = NSPredicate(format: "NOT (self IN %@)", objects);
    fetchRequest.includesPropertyValues = false; // We just need the object IDs

    let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
    try context.executeRequest(deleteRequest)

    return objects
}

from groot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.