magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

relatedByAttribute on Entity will cause memory leak #1195

Open ondev opened 8 years ago

ondev commented 8 years ago

I write a demo to repeat this issue. I have a entity named Book like: screen shot 2016-02-17 at 10 01 09 am

And request data from server, then import to DB:

- (IBAction)getBooks:(id)sender {
    NSString *url = @"http://thesg.coding.io/api/v1.0/books";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSURLResponse *response;
    NSError *error;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSArray *books = dict[@"books"];

    [MagicalRecord saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) {
        [Book MR_importFromArray:books inContext:localContext];
    }];
}

The instrument show the leaks. screen shot 2016-02-17 at 10 07 03 am

I try many times and get the conclusion: If set the relatedByAttribute's value to book_id will cause leaks. If set the relatedByAttribute's value to name have no leak.

And the last, I find because the primary key's value cause the leak, If I change the primary key's value length(<10) from server, have no leak.

{  "books": [
    {
      "name": "book1", 
      "book_id": "123456789"
    }, 
    {
      "name": "book2", 
      "book_id": "123456788"
    }
  ]
}

Hope get help from you. Thanks very much.