core data, refreshObject, multiple threads

  • I've got a main thread that handles a gui, and a worker thread with its own persistentStoreCoordinator and managedObjectContext. The gui notifies the worker thread of a change, passing it an objectID, and the worker thread gets the object, and other objects it is connected to, and calls [managedObjectContext refreshObject: on them.

    But they don't seem to get an updated object. I'm using the XML store. The store is saved, I've checked that the new values are in the XML. But refresh doesn't seem to update anything. Is there any trick to it?

          Start your day with Yahoo!7 and win a Sony Bravia TV. Enter now http://au.docs.yahoo.com/homepageset/?p1=other&p2=au&p3=tagline
  • Hi Chris,

    On 5/12/08, Chris Idou <idou747...> wrote:

    > I've got a main thread that handles a gui, and a worker thread
    > with its own persistentStoreCoordinator and
    > managedObjectContext. The gui notifies the worker thread of a
    > change, passing it an objectID, and the worker thread gets the
    > object, and other objects it is connected to, and calls
    > [managedObjectContext refreshObject: on them.
    >
    > But they don't seem to get an updated object. I'm using the XML
    > store. The store is saved, I've checked that the new values are
    > in the XML. But refresh doesn't seem to update anything. Is
    > there any trick to it?

    Yes, I believe there are some tricks and also possibly some
    cases where it just won't update anything.  Ben Trumbull was
    kind enough to contribute to a similar question I posted back in
    October.  Here is the thread: http://www.mail-archive.com/<cocoa-dev...>/msg19221.html

    In particular see his comment about the issue with small
    staleness intervals.  At the time, I thought that had solved my
    problem but I've since discovered other scenarios where
    refreshObject doesn't update anything.  Its possibly more likely
    to occur after optimistic locking errors (which, so far, I am
    unable to avoid).  But I haven't yet demonstrated that with any
    degree of scientific method.  I'm going to put it under the
    microscope again over the Christmas holidays.  Feel free to stay
    in touch about it.

    Best regards and best of luck,

    Steve
  • I've got fairly modest requirements, in so far as I'm using the XML store, and I'm happy to refresh the entire object graph of my worker thread when there is a change. So I've even tried [managedObjectContext reset] and querying from the beginning, but that doesn't seem to work either.

    I'm starting to think in terms of rebuilding the entire PersistentStoreCoordinator / ManagedObjectContext stack as a way to get new data. But a question I've had in the back of my mind for some time now, is there is no obvious way to close a NSPersistentStoreCoordinator or close a NSManagedObjectContext. Does one simply dealloc them, or in the case of garbage collection, just throw them away? Usually database type interfaces require some kind of close API

    --- On Fri, 5/12/08, Steve Steinitz <steinitz...> wrote:

    > From: Steve Steinitz <steinitz...>
    > Subject: Re: core data, refreshObject, multiple threads
    > To: <cocoa-dev...>
    > Cc: <idou747...>
    > Received: Friday, 5 December, 2008, 4:04 PM
    > Hi Chris,
    >
    > On 5/12/08, Chris Idou <idou747...> wrote:
    >
    >> I've got a main thread that handles a gui, and a
    > worker thread with its own persistentStoreCoordinator and
    > managedObjectContext. The gui notifies the worker thread of
    > a change, passing it an objectID, and the worker thread gets
    > the object, and other objects it is connected to, and calls
    > [managedObjectContext refreshObject: on them.
    >>
    >> But they don't seem to get an updated object.
    > I'm using the XML store. The store is saved, I've
    > checked that the new values are in the XML. But refresh
    > doesn't seem to update anything. Is there any trick to
    > it?
    >
    > Yes, I believe there are some tricks and also possibly some
    > cases where it just won't update anything.  Ben Trumbull
    > was kind enough to contribute to a similar question I posted
    > back in October.  Here is the thread:
    > http://www.mail-archive.com/<cocoa-dev...>/msg19221.html
    >
    > In particular see his comment about the issue with small
    > staleness intervals.  At the time, I thought that had solved
    > my problem but I've since discovered other scenarios
    > where refreshObject doesn't update anything.  Its
    > possibly more likely to occur after optimistic locking
    > errors (which, so far, I am unable to avoid).  But I
    > haven't yet demonstrated that with any degree of
    > scientific method.  I'm going to put it under the
    > microscope again over the Christmas holidays.  Feel free to
    > stay in touch about it.
    >
    > Best regards and best of luck,
    >
    > Steve

          Start your day with Yahoo!7 and win a Sony Bravia TV. Enter now http://au.docs.yahoo.com/homepageset/?p1=other&p2=au&p3=tagline
  • Chris,

    Can you make use of the - (void)refreshObject:(NSManagedObject
    *)object mergeChanges:(BOOL)flag method on NSManagedObjectContext?

    Thanks
    --
    John Clayton
    Skype: johncclayton

    On 7/12/2008, at 12:22 PM, Chris Idou wrote:

    > I've got fairly modest requirements, in so far as I'm using the XML
    > store, and I'm happy to refresh the entire object graph of my worker
    > thread when there is a change. So I've even tried
    > [managedObjectContext reset] and querying from the beginning, but
    > that doesn't seem to work either.
    >
    > I'm starting to think in terms of rebuilding the entire
    > PersistentStoreCoordinator / ManagedObjectContext stack as a way to
    > get new data. But a question I've had in the back of my mind for
    > some time now, is there is no obvious way to close a
    > NSPersistentStoreCoordinator or close a NSManagedObjectContext. Does
    > one simply dealloc them, or in the case of garbage collection, just
    > throw them away? Usually database type interfaces require some kind
    > of close API
    >
    >
    > --- On Fri, 5/12/08, Steve Steinitz <steinitz...>
    > wrote:
    >
    >> From: Steve Steinitz <steinitz...>
    >> Subject: Re: core data, refreshObject, multiple threads
    >> To: <cocoa-dev...>
    >> Cc: <idou747...>
    >> Received: Friday, 5 December, 2008, 4:04 PM
    >> Hi Chris,
    >>
    >> On 5/12/08, Chris Idou <idou747...> wrote:
    >>
    >>> I've got a main thread that handles a gui, and a
    >> worker thread with its own persistentStoreCoordinator and
    >> managedObjectContext. The gui notifies the worker thread of
    >> a change, passing it an objectID, and the worker thread gets
    >> the object, and other objects it is connected to, and calls
    >> [managedObjectContext refreshObject: on them.
    >>>
    >>> But they don't seem to get an updated object.
    >> I'm using the XML store. The store is saved, I've
    >> checked that the new values are in the XML. But refresh
    >> doesn't seem to update anything. Is there any trick to
    >> it?
    >>
    >> Yes, I believe there are some tricks and also possibly some
    >> cases where it just won't update anything.  Ben Trumbull
    >> was kind enough to contribute to a similar question I posted
    >> back in October.  Here is the thread:
    >> http://www.mail-archive.com/<cocoa-dev...>/msg19221.html
    >>
    >> In particular see his comment about the issue with small
    >> staleness intervals.  At the time, I thought that had solved
    >> my problem but I've since discovered other scenarios
    >> where refreshObject doesn't update anything.  Its
    >> possibly more likely to occur after optimistic locking
    >> errors (which, so far, I am unable to avoid).  But I
    >> haven't yet demonstrated that with any degree of
    >> scientific method.  I'm going to put it under the
    >> microscope again over the Christmas holidays.  Feel free to
    >> stay in touch about it.
    >>
    >> Best regards and best of luck,
    >>
    >> Steve
    >
    >
    > Start your day with Yahoo!7 and win a Sony Bravia TV. Enter now http://au.docs.yahoo.com/homepageset/?p1=other&p2=au&p3=tagline
  • --- On Sun, 7/12/08, John Clayton <john_clayton...> wrote:

    > From: John Clayton <john_clayton...>
    > Subject: Re: core data, refreshObject, multiple threads
    > To: <idou747...>
    > Cc: <cocoa-dev...>, "Steve Steinitz" <steinitz...>
    > Received: Sunday, 7 December, 2008, 5:45 AM
    > Chris,
    >
    > Can you make use of the -
    > (void)refreshObject:(NSManagedObject *)object
    > mergeChanges:(BOOL)flag method on NSManagedObjectContext?

    See my original posting below.

    >
    > Thanks
    > --
    > John Clayton
    > Skype: johncclayton
    >
    >
    >
    >
    > On 7/12/2008, at 12:22 PM, Chris Idou wrote:
    >
    >> I've got fairly modest requirements, in so far as
    > I'm using the XML store, and I'm happy to refresh
    > the entire object graph of my worker thread when there is a
    > change. So I've even tried [managedObjectContext reset]
    > and querying from the beginning, but that doesn't seem
    > to work either.
    >>
    >> I'm starting to think in terms of rebuilding the
    > entire PersistentStoreCoordinator / ManagedObjectContext
    > stack as a way to get new data. But a question I've had
    > in the back of my mind for some time now, is there is no
    > obvious way to close a NSPersistentStoreCoordinator or close
    > a NSManagedObjectContext. Does one simply dealloc them, or
    > in the case of garbage collection, just throw them away?
    > Usually database type interfaces require some kind of close
    > API
    >>
    >>
    >> --- On Fri, 5/12/08, Steve Steinitz
    > <steinitz...> wrote:
    >>
    >>> From: Steve Steinitz
    > <steinitz...>
    >>> Subject: Re: core data, refreshObject, multiple
    > threads
    >>> To: <cocoa-dev...>
    >>> Cc: <idou747...>
    >>> Received: Friday, 5 December, 2008, 4:04 PM
    >>> Hi Chris,
    >>>
    >>> On 5/12/08, Chris Idou <idou747...>
    > wrote:
    >>>
    >>>> I've got a main thread that handles a gui,
    > and a
    >>> worker thread with its own
    > persistentStoreCoordinator and
    >>> managedObjectContext. The gui notifies the worker
    > thread of
    >>> a change, passing it an objectID, and the worker
    > thread gets
    >>> the object, and other objects it is connected to,
    > and calls
    >>> [managedObjectContext refreshObject: on them.
    >>>>
    >>>> But they don't seem to get an updated
    > object.
    >>> I'm using the XML store. The store is saved,
    > I've
    >>> checked that the new values are in the XML. But
    > refresh
    >>> doesn't seem to update anything. Is there any
    > trick to
    >>> it?
    >>>
    >>> Yes, I believe there are some tricks and also
    > possibly some
    >>> cases where it just won't update anything.
    > Ben Trumbull
    >>> was kind enough to contribute to a similar
    > question I posted
    >>> back in October.  Here is the thread:
    >>>
    > http://www.mail-archive.com/<cocoa-dev...>/msg19221.html
    >>>
    >>> In particular see his comment about the issue with
    > small
    >>> staleness intervals.  At the time, I thought that
    > had solved
    >>> my problem but I've since discovered other
    > scenarios
    >>> where refreshObject doesn't update anything.
    > Its
    >>> possibly more likely to occur after optimistic
    > locking
    >>> errors (which, so far, I am unable to avoid).  But
    > I
    >>> haven't yet demonstrated that with any degree
    > of
    >>> scientific method.  I'm going to put it under
    > the
    >>> microscope again over the Christmas holidays.
    > Feel free to
    >>> stay in touch about it.
    >>>
    >>> Best regards and best of luck,
    >>>
    >>> Steve
    >>
    >>
    >> Start your day with Yahoo!7 and win a Sony Bravia
    > TV. Enter now
    > http://au.docs.yahoo.com/homepageset/?p1=other&p2=au&p3=tagline


          Start your day with Yahoo!7 and win a Sony Bravia TV. Enter now http://au.docs.yahoo.com/homepageset/?p1=other&p2=au&p3=tagline
  • Hi Chris,

    On 7/12/08, Chris Idou wrote:

    > I've even tried [managedObjectContext reset] and querying from the
    > beginning, but that doesn't seem to work either.

    My experience has been that that works (at least in 10.5 with GC
    activated).  I'm no Cocaa memory management expert but I suppose
    there could be a scenario where you would also have to release
    and nil all the managed objects.

    > rebuilding the entire PersistentStoreCoordinator /
    > ManagedObjectContext stack as a way to get new data

    I agree that one can get to that way of thinking after being
    shunned by refreshObject etc :) However, I suggest fiddling a
    little more with MOC reset, first -- its not as coy as refreshObject.

    Cheers,

    Steve