iOS Core Data do you have to pull out an object before you can update it?

whitedragon101

macrumors 65816
Original poster
Sep 11, 2008
1,239
261
In SQL if I want to update every value in a column if its equal to x or less than X i can just put that in a query and fire it off.
eg

UPDATE Records SET isCurrent=0 WHERE isCurrent='1'


Can I do this in Core Data (I hope so) or do I have to do a query to get every record out (as an NSManagedObject) where isCurrent=0 then iterate through setting each and every one then saving?
 

ChristianVirtual

macrumors 601
May 10, 2010
4,091
265
日本
The database structure of CoreData is private to Apple; better no messing around with direct access.
As CoreData focus on objects I think you better read them, change the values with all potential consistencies checks your code might have and commit them back.

You can also save them as bulk: one [mocontext :save] after the iteration. Should be quick enough and easy to code.

Even more if your CoreData is linked to iCloud. Changing direct will bypass all synchronization mechanisms. Not sure if relevant in your case.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,429
4,435
Pale blue comma
Core Data is fundamentally designed around object graphs.
http://en.wikipedia.org/wiki/Core_Data
Core Data is an object graph and persistence framework provided by Apple in the Mac OS X and iOS operating systems.
...​

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/coredata/Articles/cdTechnologyOverview.html
The Core Data framework provides generalized and automated solutions to common tasks associated with object life-cycle and object graph management, including persistence.
...

https://developer.apple.com/library/ios/documentation/general/conceptual/devpedia-cocoacore/ObjectGraph.html#//apple_ref/doc/uid/TP40008195-CH54-SW1
Object graph

In an object-oriented program, groups of objects form a network through their relationships with each other—either through a direct reference to another object or through a chain of intermediate references. These groups of objects are referred to as object graphs. Object graphs may be small or large, simple or complex. An array object that contains a single string object represents a small, simple object graph. A group of objects containing an application object, with references to the windows, menus and their views, and other supporting objects, may represent a large, complex object graph.
...​

If you're not thinking of your data in terms of object graphs, how to make them, persist them, update them, etc. then it's pretty unlikely that Core Data will be a good fit with your data model.

Since your question is expressed as a specific SQL statement, rather than being expressed in terms of objects and object graphs, it suggests that your data model is a poor fit with Core Data.