2010-10-04 57 views
2
@interface Week : NSManagedObject { 

} 
@property (nonatomic, retain) NSNumber *weekID; 
@property (nonatomic, retain) NSString *top; 
@property (nonatomic, retain) NSString *summary1; 
@property (nonatomic, retain) NSMutableArray *book; 

@end 

我是從服務器讀取XML文件,並把瓦萊斯在entity.Since上面的書籍來閱讀該周可多所以它被存儲在MutableArray.I製造* book可在我的.xcdatamodel文件中轉換。應堅持的NSMutableArray這是核心數據實體的財產

我的解析器看起來像這樣解析並初始化上面周實體

if ([currentTag isEqualToString:@"book"]) { 
    NSString *USGSWebLink = [currentElementValue1 stringByStandardizingPath] ; 
    [week.book addObject:USGSWebLink]; 
    [week setBook:week.book]; 
    NSError *error; 
    if (![self.managedObjectContext save:&error]) { 
    NSLog(@"%@", [error domain]); 
    } 

因爲我取從Coredata在的NSMutableArray「weekArray」以上的值和存儲這是存儲細**顯示在**的UITableView

我的程序在下面兩個函數中崩潰,崩潰點在下面兩個函數之間切換......奇怪!

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    **//sometimes program crashes at this point** 
    return [weekArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row]; 
    **// program crash at the above line.** 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

cell.textLabel.text = [object valueForKey:@"top"]; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    // Set up the cell 
    return cell; 
} 

*請告訴我我在哪裏做錯了?難道是我存儲的NSMutableArray 書在我的解析器或 NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row]; 線 因爲我的NSLog不會打印下面indexPath.row線的方式問題。

我取功能是

- (void)fetchRecords { 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Week" inManagedObjectContext:appDelegate.managedObjectContext]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entity]; //NSLog(@"fetchRecords = %@",appDelegate.managedObjectContext); 

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"weekID" ascending:YES]; 
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
    [request setSortDescriptors:sortDescriptors]; 
    [sortDescriptor release]; 

    NSError *error;  
NSMutableArray *mutableFetchResults = [[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease] ; 
    if (!mutableFetchResults) { 
    } 
if (mutableFetchResults == nil) { 
    NSLog(@"week fetch failed"); 
} 
self.weekArray = mutableFetchResults; NSLog(@"weekArray = %@",weekArray); 
    [mutableFetchResults release]; 
[request release]; 
} 

回答

0

看起來它可能是一個 '過度釋放錯誤'。在你獲取方法您有:

NSMutableArray *mutableFetchResults = 
    [[[managedObjectContext executeFetchRequest:request 
              error:&error] mutableCopy] autorelease] ; 

這與初始化的獲取結果的可變副本「mutableFetchResults」陣列。然後指定這個數組應該是自動釋放的(意味着它將在可能時被釋放)。

然後,指定「mutableFetchResults」到您的類與屬性:

self.weekArray = mutableFetchResults; 

正如你已經聲明此屬性爲「保留」,當你指定「mutableFetchResults」這個屬性的類將保留它。現在別的東西已經通過保留它來聲明這個數組的所有權,早期的'autorelease'可以自由發送'release'消息。在這一點上一切都很好,因爲你所有的保留和發佈都是平衡的。然而,在下一行你明確地再次釋放它:

[mutableFetchResults release]; 

這是不必要的,因爲它會已經被「自動釋放」釋放。這種「過度釋放」的結果是,分配給「self.weekArray」的引用現在可能指向內存中的一些其他隨機對象 - 這可能是您嘗試訪問數組時應用程序崩潰的原因。

+0

非常感謝「傑克」。刪除「[mutableFetchResults發佈]後,我的程序工作得很好;」 。我無法想象你自己。 – Ravi 2010-10-05 05:54:09