Note that there are some explanatory texts on larger screens.

plurals
  1. PONSmanaged context threads
    primarykey
    data
    text
    <p>I use a singleton for working with arrays etc. cross the views in the application. </p> <p>To initialize the singleton and the <code>NSManagedObjectContext</code>, so that I can fetch objects, I use: </p> <pre><code>+(DataControllerSingleton *)singleDataController { static DataControllerSingleton * single=nil; @synchronized(self) { if(!single) { single = [[DataControllerSingleton alloc] init]; NSManagedObjectContext *context = [single.fetchedResultsController managedObjectContext]; single.masterCareList = [[NSMutableArray alloc] init]; } } return single; } </code></pre> <p>When I insert a new object that object will not show up in display functions until I restart the application. I insert new object through this method in the singleton class:</p> <pre><code>- (void)insertNewObject:(Care *)care { NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity]; NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName: [entity name] inManagedObjectContext:self.managedObjectContext]; NSString *fileName = care.pictureURL; NSString *text = care.causeText; NSDate *date = care.date; NSData *imgData = care.imageData; [newManagedObject setValue:fileName forKey:@"urlPath"]; [newManagedObject setValue:text forKey:@"name"]; [newManagedObject setValue:date forKey:@"date"]; [newManagedObject setValue:imgData forKey:@"imageData"]; // Save the context. [self saveContext]; NSError *error = nil; if (![context save:&amp;error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } </code></pre> <p>My count method is the way I can tell that the new object is not included until I restart the application. The count method is also in the singleton as well. </p> <pre><code>- (NSUInteger)countOfList { NSArray *fetchedData = [_fetchedResultsController fetchedObjects]; return [fetchedData count]; } </code></pre> <p>When calling singleton I use: </p> <pre><code>DataControllerSingleton *singletonData = [DataControllerSingleton singleDataController]; [singletonData insertNewObject:care]; </code></pre> <p><code>managedObjectContext</code> property: </p> <p>.h: </p> <pre><code>@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; </code></pre> <p>.m:</p> <pre><code>@implementation DataControllerSingleton @synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator; </code></pre> <p>Why will not my new object show up in ex count until I restart application? </p> <p>Am I somehow using different threads with different contexts, or different fethedResultsController or different singleton (shouldnt be possible right?)??</p> <hr> <p>I added these two lines, which are not included in the genereated CoreData Stack, and it now works fine. </p> <p>In singleton header: </p> <pre><code>@interface DataControllerSingleton : NSObject &lt;NSFetchedResultsControllerDelegate&gt; </code></pre> <p>In implementation file,</p> <pre><code>(NSFetchedResultsController *)fetchedResultsController { _fetchedResultsController.delegate = self; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload