Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code lacks autoreleasepools :D this means you're storing 1000 MyEntity objects in memory, and this WILL lead to crashes and iOS will FC your app (force close). The code should look like:</p> <pre><code>for(int i = 0; i &lt; 1000; i++) { @autoreleasepool { MyEntity *object = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:[self managedObjectContext]]; object.name = [NSString stringWithFormat:@"%d", i]; object.age = [NSNumber numberWithInt:i]; } } [self managedObjectContext] save:nil]; </code></pre> <p>OR:</p> <pre><code>for(int i = 0; i &lt; 1000; i++) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MyEntity *object = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:[self managedObjectContext]]; object.name = [NSString stringWithFormat:@"%d", i]; object.age = [NSNumber numberWithInt:i]; [pool drain]; } [self managedObjectContext] save:nil]; </code></pre> <p>Also, this may not be the best way to do this kind of thing, I would suggest adding it to the context, then operating on the context itself perhaps using </p> <p><code>[[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext] name] = [NSString stringWithFormat:@"%d", i];</code> </p> <p>or something of the sort. This way you don't create a local copy of the object and clog up your memory.</p> <p>Using the last of three methods, I would do it like this:</p> <pre><code>for(int i = 0; i &lt; 1000; i++) { [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:[self managedObjectContext]]; [[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext] name] = [NSString stringWithFormat:@"%d", i]; [[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext] age] = [NSNumber numberWithInt:i]; } [self managedObjectContext] save:nil]; </code></pre> <p>And if this MyEntity is your own custom class, you can even simplify your code by using</p> <pre><code>@property(nonatomic, retain) id name; @property(nonatomic, retain) id age; </code></pre> <p>Replace <code>id name</code> and <code>id age</code> with the correct variable and object class, and this way your code will be even simpler:</p> <pre><code>for(int i = 0; i &lt; 1000; i++) { [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:[self managedObjectContext]]; [[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext] setName: [NSString stringWithFormat:@"%d", i]]; [[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext] setAge: [NSNumber numberWithInt:i]]; } [self managedObjectContext] save:nil]; </code></pre> <p>Hope this helps and doesn't confuse you!</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