Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS Core Data - Updating Multiple records
    text
    copied!<p>I've scoured and still haven't found anything that quite works. Either the question/answer is too old or it simply hasn't worked for me. This is my first attempt at "my own" app. As it seems a right of passage, I'm making a checklist app. Here's what I'm looking for:</p> <p>My Data Store contains 4 attributes: <code>name, category, isChecked, isActive</code> (more will surely follow as I expand)</p> <p>When my View Controller initially loads, the <code>NSFetchedResultsController</code> has an <code>NSPredicate</code> that only retrieves the records whose attribute <code>isActive</code> is YES (or <code>[NSNumber numberWithBool:YES</code>). It then takes those records and displays them into the appropriate cells for the user. When a user clicks on a cell, the Data Store updates and changes the <code>isChecked</code> attribute accordingly. Everything works good to this point.</p> <p>What I need to do now is to be able to remove the items (1 or more) from the list. Specifically, I need it to update the Data Store attributes <code>isChecked</code> and <code>isActive</code> to NO only if it's current <code>isChecked</code> attribute is YES. (I'm not looking to delete the record from the data store as they will be used to build up the database for the users future use.)</p> <p>I've used, among other things:</p> <pre><code>[[[self fetchedResultsController] fetchedObjects] setValue:[NSNumber numberWithBool:NO] forKey:@"isChecked"]; </code></pre> <p>This does actually work, it removes the checkmark(s) and updates the store accordingly. Problem is, not only am I making another request to the data store for the <code>isActive</code> items, it also searches the entire "Active List" that was fetched and sets each of their <code>isChecked</code> attributes to NO. This may not be too big of an issue for small lists, but as the list(s) expand this can be an issue.</p> <p>The other problem is, if I add:</p> <pre><code>[[[self fetchedResultsController] fetchedObjects] setValue:[NSNumber numberWithBool:NO] forKey:@"isActive"]; </code></pre> <p>It sets ALL of my list items to NO (as well as a second data store request within the same method.)</p> <p>So my question is: How can I get through the list, find only the items that are checked and update only those records (set both the isChecked &amp;&amp; isActive attributes = NO) whose <code>isChecked</code> attribute is YES rather than working through the entire list?</p> <p>I've tried creating a separate fetchedResultsController specifically for this buttons action, and it did work (that is to say, it didn't crash) but the debugger popped out a rather large 'Serious Application Error'. I won't post the error message as it's long and most likely irrelevant to any solution.</p> <p>Any assistance would be greatly appreciated. Thanks in advance and please be gentle :-].</p> <p><strong>EDIT</strong></p> <p>I have tried using a <code>for</code> loop, <code>for (NSString *item in fetchedResultsController)</code> but I get the error <code>...may not respond to 'countByEnumeratingWithState:objects:count'</code></p> <p>It seems a loop of sorts is what's needed here, but again, nothing I can find is relevant or it's outdated. Again, thanks for any assistance.</p> <p><strong>Edit 2</strong></p> <p>Here is the original error I got when I ran a second separate fetchRequestController for this button/method:</p> <p><code>An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 3 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)</code></p>
 

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