2011-04-08 47 views
0

我正在與核心數據的應用程序,我得到一個錯誤,我不明白:在自定義的初始化,我是否定義NSFetchedResultController與「self.controller = new_controller」所有工作正常,如果我只定義「控制器= new_controller」(不自我),錯誤「NSManagedObject已經被無效」後2個循環爲在該圖像中所描述的情況發生:興趣在此錯誤 Error訪問屬性沒有self.propertyname導致錯誤

應用部件在此描述:

  • 一個包含月份列表的表格(班級MonthListVC,圖片中的第1步)。其中 節描述月份(如4月 2011)和任何 節的唯一行顯示數據(即 月份期間發生的交易總和 )。

  • 選擇這些行的下一 表在導航推之一 控制器(類WeekTVC,步驟2中的圖像中),此表顯示 細節,每星期選擇的月份。這裏部分標題的格式是「周開始 - 週末」,該部分的唯一行是本週的交易總和。

  • 的NSManagedObjectContext經由自定義的初始化方法傳遞(檢查碼)

  • 這些表的每個數據與 NSFetchedResultController管理。

這裏對第一表選擇代碼(MonthListVC)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:indexPath.section]; 

    // get the first object for this section and get its date. 
    NSDate *datepoint = [[[sectionInfo objects]objectAtIndex:0] date]; 

    //Create next Controller and push it into Navigation 
    WeeksTVC *weeksTVC = (WeeksTVC*)[[WeeksTVC alloc] initWithContext:self.context datepoint:datepoint withTitle: [sectionInfo name] ]; 

    [self.navigationController pushViewController:weeksTVC animated:YES]; 

    [weeksTVC release]; 
} 

而這裏爲WeeksTVC定製init方法,其中,i定義 「self.controller = ...」 和其中i的代碼得到的問題是否我只寫「控制器= ...」。 「控制器」是一個綜合屬性。

-(WeeksTVC *)initWithContext:(NSManagedObjectContext *)n_context datepoint:(NSDate*)n_datePoint withTitle:(NSString*) stringTitle{ 
    self = [self init]; 

    if(self !=nil){ 

     self.title = [NSString stringWithFormat:@"History (%@)",stringTitle]; 

     //Back button setup 
     UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init]; 
     barButton.title = @"Back"; 
     self.navigationItem.backBarButtonItem = barButton; 

     //datePoint = n_datePoint; 
     self.context = n_context; 

     //Get Begin and End week information for this datePoint 
     NSDate *startDay = [[CommonHelper weekInfoFromData:[CommonHelper firstDayOfMonthForDate:n_datePoint]] objectForKey:@"begin"]; 
     NSDate *endDay = [[CommonHelper weekInfoFromData:[CommonHelper lastDayOfMonthForDate:n_datePoint]] objectForKey:@"end"]; 

     //Manage Request 
     NSFetchRequest *request = [[NSFetchRequest alloc]init]; 
     request.entity = [NSEntityDescription entityForName:@"Transactions" inManagedObjectContext:self.context]; 
     request.predicate = [NSPredicate predicateWithFormat:@"(date >= %@) && (date <= %@)",startDay,endDay]; 
     NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]; 
     NSArray *sortDescs = [NSArray arrayWithObjects:sort,nil]; 
     request.sortDescriptors = sortDescs; 

     "HERE THE PROBLEM WITH SELF***********************************************" 
     //Manage NSResultFetchedController 
     self.controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.context sectionNameKeyPath:@"byWeek" cacheName:nil]; 
     self.controller.delegate = self; 
     NSError *error = nil; 

     //Fetch 
     [self.controller performFetch:&error]; 
     "END OF PROBLEM***********************************************************" 

     [request release]; 
     [barButton release]; 
    } 
    return self; 
} 

我也彙報WeeksTVC dealloc方法,我發現,如果我沒有在這個方法中釋放背景和控制器......也沒有發生錯誤,如果我在init()方法寫入「控制器=」而不是「 self.controller =「

- (void)dealloc { 
    //[self.datePoint release]; 
    [self.controller release]; 
    [self.context release]; 
    [super dealloc]; 
} 

爲什麼self.controller和控制器在這種情況下是如此不同?
在init方法我曾經寫沒有自我-.-」

回答

0

在丘斯托init方法伊娃的WeeksTVC我寫

self = [self init]; 

,而不是

self = [super init]; 

這導致問題...但我很想知道究竟發生了什麼......