【问题标题】:App Crashing- Core Data Problem Or Memory Management?应用程序崩溃 - 核心数据问题或内存管理?
【发布时间】:2011-04-12 05:21:24
【问题描述】:

控制台消息:

不得不使用图片,因为在帖子中的格式不正确。

收到的内存警告级别 2 在应用崩溃之前出现。

错误出现在这一行 - cell.textLabel.text = tempRoutine.name;

链接到全尺寸图片 (http://www.box.net/shared/static/7igj3r4trh.png)

视图控制器:

@implementation RoutineTableViewController

@synthesize tableView;
@synthesize eventsArray;
@synthesize entered;
@synthesize managedObjectContext;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    if (managedObjectContext == nil) 
    { 
        managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    }

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }
    [self setEventsArray:mutableFetchResults];
    [mutableFetchResults release];
    [request release];

    UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)];
    [self.navigationItem setLeftBarButtonItem:addButton];
    [addButton release];

    UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit)];
    self.navigationItem.rightBarButtonItem = editButton;
    [editButton release];

    [super viewDidLoad];
}

- (void)viewDidUnload
{
    self.eventsArray = nil;
    [super viewDidUnload];
}

-(void)toggleEdit
{
    [self.tableView setEditing: !self.tableView.editing animated:YES];

    if (self.tableView.editing)
        [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
    else
        [self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
}

- (void)dealloc
{
    [managedObjectContext release];
    [eventsArray release];
    [entered release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark -
#pragma mark Add an event

-(void)addEvent
{    
    Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];

    routine.name=entered;

    NSError *error = nil;
    if (![managedObjectContext save:&error]) {
        // Handle the error.
    }
    NSLog(@"%@", error);

    [eventsArray insertObject:routine atIndex:0];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

-(void)showPrompt
{
    AlertPrompt *prompt = [AlertPrompt alloc];
    prompt = [prompt initWithTitle:@"Add Workout Day" message:@"\n \n Please enter title for workout day" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Add"];
    [prompt show];
    [prompt release];
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{


    if (buttonIndex != [alertView cancelButtonIndex])
    {
        entered = [(AlertPrompt *)alertView enteredText];

        if(eventsArray && entered)
        {
            Routine *tempRoutine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext]; 
            tempRoutine.name = entered;
           // Routine *tempRoutine = [[Routine alloc]init];
            //tempRoutine.name = entered;
            [eventsArray addObject:tempRoutine];
            [tempRoutine release];
            [tableView reloadData];
            [self addEvent];
        }
        /*
        if(eventsArray && entered)
        {
            [eventsArray addObject:entered];
            [tableView reloadData];
            [self addEvent];
        }
        */
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [eventsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    // Dequeue or create a new cell.

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    Routine *tempRoutine = (Routine *)[eventsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = tempRoutine.name;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {

     if (editingStyle == UITableViewCellEditingStyleDelete) {

         // Delete the managed object at the given index path.
         NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row];
         [managedObjectContext deleteObject:eventToDelete];

         // Update the array and table view.
         [eventsArray removeObjectAtIndex:indexPath.row];
         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

         // Commit the change.
         NSError *error = nil;
         if (![managedObjectContext save:&error]) {
             // Handle the error.
         }
     }
 }

【问题讨论】:

  • 您好 Faisal,您检查一下 tempRoutine.name 的值是什么?
  • 好的,我现在做一个 NSLog 并检查。
  • 好的,看看有什么价值来告诉我。
  • 我在用户点击添加按钮后设置了一个 NSLog,我得到了2011-04-12 02:13:14.275 Curl[133:707] tempRoutine.name is: 1977840。添加几个单元格后,应用程序仍然冻结。还复制单元格等。
  • 您好,还请检查 eventsArray 上的所有对象值。并检查每个索引的 tempRoutine 的值。 tempRoutine.name 是 int 还是 string?

标签: iphone objective-c memory core-data


【解决方案1】:

当您创建例程时,您使用insertNewObjectForEntityForName:inManagedObjectContext: 创建它们。然后你释放它们。但是insertNewObjectForEntityForName:inManagedObjectContext: 不会根据memory management rulesmethod's documentation 返回您拥有的对象,因此您不应该释放。

【讨论】:

  • 好的,我在哪里发布它?我应该删除发布代码?
  • @Faisal:在alertView:willDismissWithButtonIndex: 中,您应该删除[tempRoutine release] 行。
  • 感谢修复了我认为的崩溃,我只是添加了我完成的最多的单元格,通常它在 2 之后崩溃。但双重添加仍然存在。我注意到它将一个单元格添加到第一个位置,并将同一个单元格添加到最后一个位置。有什么想法吗?
  • 我想我成功了,我在 addevent 和 dissmiss alert 方法中有重复的 insertobject 代码
  • 好的,所以我在添加新单元格时删除了重复的单元格,方法是从 addEvent 方法中删除插入对象代码。但是,当我重新启动应用程序时,它会复制所有单元格。这里有什么建议吗?
【解决方案2】:

嗨,

您的表格视图是否一次显示大量数据?

我认为这是您的代码的唯一问题。如果您一次调用的行数超过 50 到 60 行,并且您通过上下滚动查看相同的行,则可能会导致内存警告。

使用 Lazy loading 的最佳方式是调用原始数据的数量,就像您一次只能加载 20 个原始数据一样,然后在用户向下滚动时加载另一行。

discussion 同样不错。您没有使用图像仍然有许多图像可能导致应用程序崩溃。

希望这能解决您的问题。

【讨论】:

  • 实际上表中只有2-3行数据。最初有 0 个,直到用户添加自己的。
猜你喜欢
  • 2011-08-24
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
相关资源
最近更新 更多