• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Basic Coredata / threading question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Basic Coredata / threading question


  • Subject: Basic Coredata / threading question
  • From: Walt Horat <email@hidden>
  • Date: Fri, 6 Apr 2007 12:26:02 -0700

I have an application that keeps a large model. The app fires off a number of network queries in parallel - each of these is fired on a separate thread. Each of these threads needs to access the model when the response returns, and execute a fetch which will require coredata to touch nearly every object in the model.

It seems that if each thread is given its own managed object context, the app will have N copies of the model all in memory at the same time - this does not sound like a good strategy for large datasets.

As a solution, I have tried implementing each thread so as to send a dictionary of non-managed objects over to the main thread (using NSObject performSelectorOnMainThread) to actually do the work. This seems to work, until the next time the app. tries to save the model - it deadlocks inside of the call to save. It is unclear whom is locking what as I can see that all of the network threads have already completed.

I am considering trying to simplify the design doing what I am told is bad - to have the threads share the same managed object context. It seems to my limited understanding of core data, that by using fine grained locking, this could be made to work.

I am considering re-coding all the core data accessors as follows:

- (NSNumber *)foo
{
    NSNumber * tmpValue;

    [[self managedObjectContext] lock];
    [self willAccessValueForKey: @"foo"];
    tmpValue = [self primitiveValueForKey: @"foo"];
    [self didAccessValueForKey: @"foo"];
    [[self managedObjectContext] unlock];

    return tmpValue;
}

- (void)setFoo:(NSNumber *)value
{
    [[self managedObjectContext] lock];
    [self willChangeValueForKey: @"foo"];
    [self setPrimitiveValue: value forKey: @"foo"];
    [self didChangeValueForKey: @"foo"];
    [[self managedObjectContext] unlock];
}

It seems that this should have the effect of serializing all access to the model, as whenever any thread dips into the model through managed object accessor methods it will lock (or block) appropriately.

Any advice, suggestions or strategies greatly appreciated.

Regards,


Walt at soundflavor dot com -----

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Basic Coredata / threading question
      • From: Jim Correia <email@hidden>
    • Re: Basic Coredata / threading question
      • From: Scott Stevenson <email@hidden>
References: 
 >A question about formatters/validation (From: Shamyl Zakariya <email@hidden>)

  • Prev by Date: Re: WebService requests returning nil
  • Next by Date: Re: Core Data Relationships
  • Previous by thread: A question about formatters/validation
  • Next by thread: Re: Basic Coredata / threading question
  • Index(es):
    • Date
    • Thread