我正在使用iOS上的xmpp框架进行聊天应用程序,我正在使用框架本身提供的名册存储,我在运行应用程序时使用我的帐户登录时收到此错误,它突然崩溃,我试图调试它,但我无法找到解释。这是代码:
@interface FriendListTableViewController ()<NSFetchedResultsControllerDelegate>
@property (nonatomic,strong) NSFetchedResultsController *fetchedResultController;
@end
@implementation FriendListTableViewController
-(AppDelegate *)appDelegate
{
return (AppDelegate *)[[UIApplication sharedApplication]delegate];
}
#pragma mark - FetchedResult Controller delegate
-(NSFetchedResultsController *)fetchedResultController
{
if (self.fetchedResultController == nil) { //Here is the EXC_BAD_ACCESS code=2
NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_roster];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoraDataStorageObject" inManagedObjectContext:moc];
NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sortDes2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDes1,sortDes2, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:10];
self.fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:moc sectionNameKeyPath:@"sectionNum" cacheName:nil];
[self.fetchedResultController setDelegate:self];
NSError *error = nil;
if (![self.fetchedResultController performFetch:&error]) {
}
}
return self.fetchedResultController;
}
希望你能帮助我。
答案 0 :(得分:0)
您正在编写一个getter方法,fetchedResultController
(虽然我会将其命名为fetchedResultsController
,以匹配类名。然后在该getter方法中,您调用self.fetchedResultController
(重新调用内部的getter)以及稍后self.fetchedResultController = ...
,在getter中调用setter。
在setter / getter方法中,你应该直接使用支持ivar。
更好的习语: 在.h:
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController
然后在.m
-(NSFetchedResultsController *)fetchedResultsController
{
if (_!fetchedResultsController) {
NSManagedObjectContext *moc = self.managedObjectContext_roster;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoraDataStorageObject" inManagedObjectContext:moc];
NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sortDes2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = @[sortDes1, sortDes2];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:10];
_fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:moc
sectionNameKeyPath:@"sectionNum"
cacheName:nil];
_fetchedResultsController.delegate = self;
NSError *error = nil;
if (![_fetchedResultsController performFetch:&error]) {
NSAssert(NO, @"fetchedResultsController fetch failed");
}
}
return _fetchedResultsController;
}
其他几点说明:我对数组初始化语法进行了现代化。我还建议你将MOC参考保留在本地,而不是回到应用代表那里找到它。混合使用点和括号表示法会使您的代码难以阅读;考虑fetchRequest.entity = entity
等等。最后,在代码中使用那些硬编码的字符串密钥路径(sectionNum
,XMPPUserCoreDataStorageObject
等)是一个难以发现的错误。将它们定义为extern
个字符串。