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

Discussion in 'iOS Programming' started by whitedragon101, Feb 21, 2014.

  1. whitedragon101 macrumors 6502a

    Joined:
    Sep 11, 2008
    #1
    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?
     
  2. ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #2
    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.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    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...l/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....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.
     
  4. ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #4
    ^^^^^

    A much better explanation including the conceptual background.
     

Share This Page