0

I have a class called SLCollectionViewModel that, if initiated, will fetch the latest entry with Core Data. So far there is no problem with the initial view. The Collection View will display the visible entries correctly (and also shows them in log).

But the problem occurs when I try to scroll the view, or try to call [self.collectionView reloadData] explicitly. The error log is:

-[_PFArray retain]: message sent to deallocated instance 0x8de9ab0

I think my self.viewModel object is deallocated somewhere, but I can’t pinpoint what’s wrong with my approach. Here is my - (void)viewDidLoad method:

- (void)viewDidLoad
{
  [super viewDidLoad];

  self.viewModel = [SLCollectionViewModel new];

  [self.collectionView reloadData];

}

And here is my SLCollectionViewModel class:

- (instancetype) init {

  self = [super init];
  if(!self) return nil;

  _managedObjectContext = [[SLCoreDataStack defaultStack] managedObjectContext];

  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
  NSEntityDescription *entity = [NSEntityDescription
                                      entityForName:@"SLPost"
                                      inManagedObjectContext:_managedObjectContext];

  [fetchRequest setEntity:entity];
  // The _posts in an NSArray that saves all my NSManageObject
  // which will be used to populate the collection view.
  _posts = [_managedObjectContext executeFetchRequest:fetchRequest
                                                error:nil];

  return self;
}
sayzlim
  • 275
  • 1
  • 2
  • 16
  • How is the `@property` for `self.viewModel` declared? – Rich Apr 27 '14 at 11:05
  • It’s `@property (nonatomic, strong) SLCollectionViewModel *viewModel;` – sayzlim Apr 27 '14 at 11:05
  • When the program halts and activates the debugger what is the output `po`ing the memory address it says was deallocated? i.e. from your original question `po 0x8de9ab0`. Also out of interest is the crash consistent, with the same error message each time? – Rich Apr 27 '14 at 11:08
  • The crash is consistent, with the same error, but different address. The output for `po 0x90c0000` is `-[_PFArray respondsToSelector:]: message sent to deallocated instance 0x90c0000 151781376` – sayzlim Apr 27 '14 at 11:11
  • Turn on [`NSZombie`s](http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode) and then update your answer with the results – Rich Apr 27 '14 at 11:16
  • I think I’ve enabled NSZombie in Diagnostic tab. But there is no differences with the result shown in log. Do I have to use **Instruments** for this? – sayzlim Apr 27 '14 at 11:30
  • You shouldn't need to, but see what it produces. – Rich Apr 27 '14 at 11:37

1 Answers1

0

I realize this is my own mistake for not creating the property properly. In my SLCollectionViewModel, the @property for NSArray *posts should use retain instead of assign.

Before: @property (nonatomic, assign) NSArray *posts;

After:

@property (nonatomic, retain) NSArray *posts;

sayzlim
  • 275
  • 1
  • 2
  • 16