ОК, поэтому у меня есть две сущности в моей модели CoreData ... они оба являются пользовательскими классами ...
Первый подкласс ManagedObject просто имеет ОДНУ переменную, называемую "мм" ...
@interface length : NSManagedObject {
@private
}
@property (nonatomic, retain) NSNumber * mm;
@end
#import "length.h"
...
@implementation length
@dynamic mm;
@end
... хорошо, другой подкласс ManagedObject называется «Times» и имеет намного больше переменных, но ничего странного.
ОК. У меня всего ОДИН управляемый объект для всего приложения.
У меня есть три разных объекта, которые делают выборки. Все они имеют свои собственные переменные экземпляра для материала выборки, единственное, что они разделяют, - это сам managedObjectContext и его persistentStoreCoordinator.
Проблема в том, что выборки для одного объекта возвращают массивы, которые содержат ДРУГОЙ объект !!! НО ПОЧЕМУ?!?!
Посмотрите, вот как я выполняю свои fetchRequests для класса "length":
- (void)setUpLengths {
NSLog(@"Setting up focal lengths");
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"length" inManagedObjectContext:MOC];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *myFetch = [MOC executeFetchRequest:fetchRequest error:&error];
if(myFetch == nil) {
//handle error
}
length *thisNewLength = nil;
//thisNewLength = [[FocalLength alloc] initWithEntity:entity insertIntoManagedObjectContext:MOC]; (commented out this line to fix memory leak, but still had same problem when it was uncommented and released at the bottom of the method)
int x;
int l = [myFetch count];
NSLog(@"lengths count: %i",l);
for(x = 0; x < l; x++) {
thisNewLength = [myFetch objectAtIndex:x];
NSString *newEntry = [NSString stringWithFormat:@"%@",thisNewLength.mm];
if([thisNewLength.mm intValue])
[main_view_controller addLength:newEntry];
NSLog(@"Added FL: %@",newEntry);
}
[fetchRequest release];
}
...
и вот как я делаю это для «Times» (табличный дисплей):
- (UITableViewCell *)tableView:(UITableView *)thisTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [thisTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Time *time = [fetchedResultsController objectAtIndexPath:indexPath];
[self configureCell:cell withTime:time];
//[CellIdentifier release];
//fetchedResultsController = nil;
return cell;
}
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Time" inManagedObjectContext:MOC];
[fetchRequest setEntity:entity];
NSError *err;
if(![MOC countForFetchRequest:fetchRequest error:&err]) {
//handle error
}
int count = [MOC countForFetchRequest:fetchRequest error:&err];
self.current_count = count;
if(count == NSNotFound) {
//Handle error
}
//NSLog(@"Times object count: %i", count);
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:MOC sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
//[fetchRequest release]; set to autorelease
[aFetchedResultsController release];
[sortDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] withTime:anObject];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void) tableView:(UITableView *)myTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Time *time = [[self fetchedResultsController] objectAtIndexPath:indexPath];
// selected_time = time; commented 7-11
selected_time = [self.fetchedResultsController objectAtIndexPath:indexPath]; //more direct
//fetchedResultsController = nil;
doneButtonItem.title = @"Use";
}
- (void)tableView:(UITableView *)myTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object for the given index path
Time *touched_time = [fetchedResultsController objectAtIndexPath:indexPath];
NSNumber *slot = touched_time.slot;
//NSLog(@"SLot: %@",slot);
if(current_count < 6) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Improper Deletion"
message:@"There must be at least five times."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
self.doneButtonItem.title = @"Done";
} else if([slot intValue] != -1) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Improper Deletion"
message:@"You can't delete a time currently assigned to a main slot."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
self.doneButtonItem.title = @"Done";
} else {
[MOC deleteObject:touched_time];
// Save the context.
NSError *error = nil;
if (![MOC save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
--current_count;
self.doneButtonItem.title = @"Done";
}
//fetchedResultsController = nil;
//[slot release]; didn't alloc so why releasE?
}
}
..
ТАК ПОЧЕМУ, когда я добавляю новую "длину", она отображается как первый элемент в таблице "Время" ?! Пожалуйста, скажите мне, потому что я нахожусь в моем конце здесь. Когда я подумал, что понимаю CoreData, он делает что-то странное и заставляет меня думать, что я, должно быть, безумен. Я пробовал чистую, чистую папку, два разных устройства и симулятор, та же проблема. Запрос на получение данных, кажется, не работает. Как мне это исправить?