简体   繁体   English

持久化NSMUtablearray是核心数据实体的属性

[英]Persisting NSMUtablearray which is property of Core Data Entity

@interface Week : NSManagedObject { 

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

@end

I'm reading XML file from server and putting vales in above entity.Since Books to be read for that WEEK can be multiple so it is stored in MutableArray.I made *book to be transformable in my .xcdatamodel file. 我正在从服务器读取XML文件并将vales放在上述实体中。由于该周要读取的书籍可能是多个,因此它存储在MutableArray中。我使* book可在我的.xcdatamodel文件中进行转换。

My parser looks like this which parse and initialize above Week entity . 我的解析器看起来像这样,它在Week实体上方进行解析和初始化。

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]);
   }

This is storing fine because I fetch the above value from Coredata and store in NSMutableArray "weekArray"**to be displayed in **UITableView . 这存储得很好,因为我从Coredata获取了上面的值并存储在NSMutableArray“ weekArray” **中,以显示在UITableView中

My program crashes in below two functions and point of crash toggles between below 2 functions...strange!!! 我的程序在以下两个函数中崩溃,崩溃点在以下两个函数之间切换...奇怪!!!

- (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;
}

*Pls tell me where I'm doing wrong ? *请告诉我我在哪里做错了? Is it problem in the way I'm storing NSMutableArray book in my parser or NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row]; 在解析器或NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row];存储NSMutableArray 书籍的方式是否有问题NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row]; line Because my NSLog does not print below indexPath.row line. 行,因为我的NSLog不打印在indexPath.row行下面。

My fetch function is : 我的提取函数是

- (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];  
} 

Looks like it might be an 'over-releasing bug'. 看起来这可能是“过度释放的错误”。 In your fetch method you have: 在您的提取方法中,您具有:

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

Which initialises the 'mutableFetchResults' array with a mutable copy of the result of the fetch. 这会使用获取结果的可变副本初始化“ mutableFetchResults”数组。 You then specify that this array should be autoreleased (meaning it will be released when it can be). 然后,您指定此数组应自动释放(这意味着它将在可能的情况下释放)。

You then assign the 'mutableFetchResults' to a property of your class with: 然后,您可以使用以下方法将“ mutableFetchResults”分配给您的类的属性:

self.weekArray = mutableFetchResults;

As you've declared this property as 'retain', when you assign 'mutableFetchResults' to this property your class will retain it. 正如您已将此属性声明为“保留”一样,当您将“ mutableFetchResults”分配给该属性时,您的类将保留它。 Now that something else has claimed ownership of this array by retaining it, the earlier 'autorelease' is free to send the 'release' message. 现在,其他人已经通过保留该数组来声明了该数组的所有权,较早的“ autorelease”可以自由发送“ release”消息。 At this point everything is fine as all your retains and releases are balanced. 在这一点上,一切都很好,因为您所有的保留和释放都是平衡的。 However, on the next line you explicitly release it again with: 但是,在下一行,您可以使用以下命令再次明确地释放它:

[mutableFetchResults release];

This is unnecessary as it'll already have been released by the 'autorelease'. 这是不必要的,因为它已经由“ autorelease”发布了。 The result of this 'over-release' is that the reference assigned to 'self.weekArray' may now be pointing to some other random object in memory - which may well be the reason that your application is crashing when you try to access the array. 这种“过度释放”的结果是,分配给“ self.weekArray”的引用现在可能指向内存中的其他随机对象-这很可能是您尝试访问数组时应用程序崩溃的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM