我有一个UitabarController。其中一个选项卡显示应用程序“书签”,这些书签基本上是保存到核心数据(sqllite)数据库的搜索类型。
加载应用程序时,转到书签视图(
bookmarksViewController
)它在我的AppDelegate中加载函数(
getBookmarks
)并将结果显示在表中。这是完美的,可以在视图之间切换,在其他视图中执行搜索切换回来。没问题。它每次都加载内容。
但是…当我加载一个搜索视图并添加一个新书签,然后切换回书签视图时,它会随着错误消息而消失。”
EXC_BAD_ACCESS
“。
我不知道为什么以及如何解决这个问题。
这是我的代码:
书签view controller.m
[...]
- (void)viewDidLoad {
[super viewDidLoad];
theBookmarks = nil;
// Set up the edit and add buttons.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self setTitle:NSLocalizedString(@"Bookmarks", @"bookmarksViewController")];
}
- (void)viewWillAppear:(BOOL)animated {
if (theBookmarks != nil)
{
NSLog(@"Release it!");
[theBookmarks release];
}
NSLog(@"Appear 1");
stationenAppDelegate *stationen = (stationenAppDelegate *)[[UIApplication sharedApplication]delegate];
NSLog(@"Appear 1 - stage 2");
theBookmarks = [[stationen getBookmarks] retain];
NSLog(@"Appear 2");
[super viewWillAppear:animated];
}
[...]
MyAppDealth.m
[...]
/**
* Bookmarks
*
* Type definition
* 1: Station search
* 2: Train search
* 3: Interuption search
*/
- (NSMutableArray*)getBookmarks
{
NSLog(@"check 1");
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"bookmarks"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSLog(@"check 2");
NSError *error;
NSArray *items = [[self.managedObjectContext
executeFetchRequest:fetchRequest error:&error] retain];
NSMutableArray *returnArray = [[[NSMutableArray alloc] initWithArray:items] retain];
NSLog(@"check 4");
[fetchRequest release];
return returnArray;
}
- (void)addBookmark:(bookmarks_object*)theBookmark
{
BOOL exists = [self checkIfBookmarkExistsUsingBookmark:theBookmark];
if(!exists)
{
bookmarks *saveBookmark = (bookmarks *)[NSEntityDescription insertNewObjectForEntityForName:@"bookmarks"
inManagedObjectContext:self.managedObjectContext];
[saveBookmark setStation_from:theBookmark.station_from];
[saveBookmark setStation_to:theBookmark.station_to];
[saveBookmark setAddDate:[[NSDate alloc] init]];
[saveBookmark setSort:theBookmark.sort];
[saveBookmark setPlace:[[NSNumber alloc] initWithInt:([[self getBookmarks] count]+1)]];
[saveBookmark setName:([theBookmark.type isEqualToString:@"1"] || [theBookmark.type isEqualToString:@"2"] ? theBookmark.station_from : [[NSString alloc] initWithFormat:@"%@ -> %@", theBookmark.station_from, theBookmark.station_to])];
[saveBookmark setType:theBookmark.type];
[self saveAction];
[saveBookmark release];
}
else {
NSLog(@"No need to add, %@ already exists!", theBookmark.station_from);
}
}
- (BOOL)checkIfBookmarkExistsUsingBookmark:(bookmarks_object*)theBookmark
{
// Get the categories
NSArray *historyArray = [self getBookmarks];
BOOL exists = NO;
for(bookmarks *dbHistory in historyArray)
{
if ([theBookmark.type isEqualToString:@"1"] || [theBookmark.type isEqualToString:@"2"])
{
if([[dbHistory station_from] isEqualToString:theBookmark.station_from])
{
exists = YES;
continue;
}
}
else if ([theBookmark.type isEqualToString:@"3"])
{
if([[dbHistory station_from] isEqualToString:theBookmark.station_from] &&
[[dbHistory station_to] isEqualToString:theBookmark.station_to])
{
exists = YES;
continue;
}
}
else {
NSLog(@"Error! Unknown type!");
return NO;
}
}
return exists;
}
[...]
堆栈跟踪(流:打开应用程序,加载书签视图,切换到搜索视图,添加书签,切换回)
2010-09-19 13:51:54.554 stationen[7256:207] Appear 1
2010-09-19 13:51:54.555 stationen[7256:207] Appear 1 - stage 2
2010-09-19 13:51:54.555 stationen[7256:207] check 1
2010-09-19 13:51:54.560 stationen[7256:207] check 2
2010-09-19 13:51:54.562 stationen[7256:207] check 4
2010-09-19 13:51:54.562 stationen[7256:207] Appear 2
2010-09-19 13:52:26.669 stationen[7256:207] check 1
2010-09-19 13:52:26.670 stationen[7256:207] check 2
2010-09-19 13:52:26.671 stationen[7256:207] check 4
2010-09-19 13:52:26.671 stationen[7256:207] No need to add, 230 already exists!
2010-09-19 13:52:30.509 stationen[7256:207] Release it!
2010-09-19 13:52:30.510 stationen[7256:207] Appear 1
2010-09-19 13:52:30.510 stationen[7256:207] Appear 1 - stage 2
Program received signal: âEXC_BAD_ACCESSâ.