Is it possible to do mass updates on a given entity in Core Data?
开发者_Python百科Given an Person entity for example, can I do something like this:
Person.update(@"set displayOrder = displayOrder + 1 where displayOrder > 5")
Or is my only option to fetch all the entities needed and then loop through and update them individually???
Thanks
You're thinking of Core Data as SQL. Big mistake.
Entities are not tables and instances are not records. Each instance is its own object so you do have to change them individually. Otherwise, there wouldn't be much point to using an object graph.
Edit01:
How then would you handle the following UI problem: You want to present a table of Person entities ordered by "name." BUT ... the user can re-order the Person entities shown in this table AND you want that persisted so that the entities are now sorted by "name" + "displayOrder" (or whatever you want to call it). Where do you maintain the value of "displayOrder" if not as an attribute of the Person entity?
Oh, I see what your trying to do. You making it a lot harder than it has to be. Core Data handles this type of function easily.
Sort orders are not (usually, see below) facets of the data model but rather the view controller i.e. they're just temporary orders used to display data to the user in a meaningful manner. As such they are created on the fly as needed. In this case you would create a NSSortDescriptor/s to sort on the attribute keys you wish. Then set your fetch request to use those sort descriptors. Then your fetch would return an array with the elements in the desired order.
No fuss no muss.
If the user wants a different sort order just make a different group of sort descriptors. There is no reason to include the sort logic in the actual data model itself and there are many reasons not to do.
Separating the actual data from the display is very important in the Model-View-Controller design pattern on with all the iPhone API is based. You don't want to include display logic in the data model and you don't want to store data or have data manipulating logic in the display.
Edit02:
In my case users will need to be able to come back to the application, view the table and see the Person entities in the order THEY put them. How else can you do this except by having a displayOrder attribute?
If you do want to save the order as user data then you do need to include it in the data model.
By coincidence, I just had such a need. Here's how I handled the problem for a small number of objects. These are the methods of NSMangedObject subclass. This works for several hundred lightweight objects.
If you have a large number of heavy weight objects (thousands) I suggest creating a lightweight entity to implement a linked list. Call it OrderEntity. It would have an index attribute, a relationship to the indexed entity, a relationship to the previous OrderEntity and a relationship to the next. (Using a lightweight entity prevents you from having to fault heavy objects which saves memory and makes everything faster. You only have to fault the indexed object when you need to display it.)
Then you use the standard linked list methods to insert, swap and move the items in the list. Then you call update on the moved objects and they call all subsequent OrderEntities to update their indexes.
It can be as simple as having a method that calls the next OrderEntity, passes the current objects index and tells the next to set its order to passedIndex+1. That is basically the exact function as the SQL statement you want to mimic (because of course the logic is the same.)
If you have to do this a lot, create a NSManagedObject subclass to handle the indexing and then have your indexed classes inherit from that.
No, you can't do "bulk" updates in Core Data. If you think you want to do this, you're fundamentally missing the point of Core Data: it is not a relational data engine, it is a graph management framework engine. Bulk updates like you describe are what you do with tables of data. Iterating through an object graph is what you do with, well an object graph management engine.
When your data is really tabular (as yours may be), Core Data may not be the best option. The bulk update issue was one of the reasons Brent Simmons has switched away from Core Data for some applications. As always, use the right tool for the right job.
In this case, there is probably a better way to do what you want. Display order is not really a model property; it's more properly a view property. You probably want to create a fetched property that sorts on something properly in the model. If that's a date, have a date property. If it's a display order preference, you may want to keep the items in a linked-list style graph where each instance has a prev/next relationships. Updating this is easy, and displaying them (a sequential operation) is easy as well.
The correct way to do that would be to get back an array of all Person objects (just make a fetch request for all Person entities without specifying a predicate).
Then use the NSArray method makeObjectsPerformSelector:withObject: to apply a bulk change - you may need to create a custom method on the object to have it perform the exact change required.
Then call save on your NSManageObjectContext, and your changes will be stored.
As others noted, it's not like SQL where you issue textual updates - but it doesn't mean yu can't still do batch changes.
It is not possible to do bulk SQL-like updates using Core Data, you will have to loop through all of your objects to update the values.
If you want a displayOrder attribute that is saved, you do need to store it as an attribute in the data model. This is such a common problem I am amazed that there isn't a built-in solution for it yet.
You cannot do this, at least not without circumventing the entire Core Data object hierarchy (which is really really really not recommended).
精彩评论