Comments (8)
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.
Nice, clear and concise. Thanks a lot! ;-)
from groot.
No problem. I will think about adding this functionality, but in the meantime you can just use that code.
from groot.
Reopening so that it's visible to everyone.
from groot.
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.
You can setup a delete rule in your relationship to cascade deletes. Check the Core Data Inspector with your items
relationship selected.
from groot.
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.
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)
- Ralationships with objects
- Swift3 HOT 3
- Update NSManagedObject with JSON
- Add relationships by merging with existing ones HOT 2
- Groot.swift compile errors
- Xcode 8 \ Swift 2.3 Support
- Groot cannot parse HOT 1
- Question About Concurrency Type for Context HOT 2
- Ambiguous use of jsonDictionary(from:) HOT 1
- How to use ValueTransformer in swift3? can you give me a sample pplease HOT 5
- Could not locate the entity for myappTests.Example when inside XCTestCase HOT 1
- Prevent infinite recursion when Groot traverses complex data model? HOT 6
- 【JSONKeyPath】
- GRTUniquingSerializationStrategy: attribute with null value replaces original value HOT 3
- Serialised output field names not in quotes HOT 2
- Could not locate the entity for \(className) even though it exists HOT 5
- Parsing Boolean value fails
- Ability to modify objects after being imported using Groot HOT 2
- Automatically mapping properties with the same name
- how to convert Json to JsonDictionary?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from groot.