Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrecognized selector mutableCopyWithZone

I am following the tutorial for Core Data from this site http://www.appcoda.com/introduction-to-core-data/

But I am getting the following error:

2016-06-23 17:55:11.905 MyStore[6020:596233] -[NSAsynchronousFetchResult mutableCopyWithZone :]: unrecognized selector sent to instance 0x7f8950e12eb0

I placed a breakpoint and the error seems to come from the following procedure.

- (void) viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    //Fetch the devices from the persistent store.
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
    self.devices = [[managedObjectContext executeRequest:fetchRequest error:nil] mutableCopy]; //error here
    
    [self.tableView reloadData];
}

I have the property "devices" declared as a NSMutableArray.

Any type of help will be appreciated.


2 Answers

So I was using the incorrect method when I was following the procedure.

The method I should have used was executeFetchRequest and by mistake I was using executeRequest. The first one returns indeed an NSArray but the other one returns a NSPersistentStoreResult.

Here are are the methods.

- (nullable NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error;
- (nullable __kindof NSPersistentStoreResult *)executeRequest:(NSPersistentStoreRequest*)request error:(NSError **)error NS_AVAILABLE(10_10, 8_0);

Thanks to the help of Volodymyr I was able to check that my object returned was indeed the type I was expecting which was not and from there I could have changed the method but then I discovered that the object returned was a more complex one and has an NSArray property I could use.

So here is my code with my tests :)

- (void) viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    //Fetch the devices from the persistent store.
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];

    id whatAreYou = [managedObjectContext executeRequest:fetchRequest error:nil];
    NSLog(@"%@", [whatAreYou class]); // turns out you are a NSPersistentStoreResult
    // lucky for me you are have finalResult property that returns an NSArray. :)

    self.devices = [[whatAreYou finalResult] mutableCopy]; //no more errors here :)

    [self.tableView reloadData];
}

Thanks a lot for the help. Checking for the object type is something I will add to my debugging tools from now on :)

Under mutableCopy is layering mutableCopyWithZone. Thats mean that object that returns from your fetch request does not implements method mutableCopyWithZone. You can try to use instead copy method. Still you need to check what object was return and if this object implements methods mutableCopyWithZone or copyWithZone.

like image 25
Volodymyr Avatar answered Sep 21 '26 22:09

Volodymyr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!