Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将保存对象的NSFetchResultController数组更改为对象的不同索引?_Ios_Objective C_Uitableview_Core Data_Nsfetchedresultscontroller - Fatal编程技术网

Ios 如何将保存对象的NSFetchResultController数组更改为对象的不同索引?

Ios 如何将保存对象的NSFetchResultController数组更改为对象的不同索引?,ios,objective-c,uitableview,core-data,nsfetchedresultscontroller,Ios,Objective C,Uitableview,Core Data,Nsfetchedresultscontroller,我有一种方法,用于将表视图中的单元格保存并移动到不同的索引,如下所示: - (void)moveTableView:(FMMoveTableView *)tableView moveRowFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { NSArray *allObjects = [self.fetchedResultController fetchedObjects]; //

我有一种方法,用于将表视图中的单元格保存并移动到不同的索引,如下所示:

- (void)moveTableView:(FMMoveTableView *)tableView moveRowFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
NSArray *allObjects = [self.fetchedResultController fetchedObjects];
//array to hold the new order    
NSMutableArray * arrayCorectOrder = [NSMutableArray new];
//the target object that is in the index we are moving from
Target *target = [self.fetchedResultController objectAtIndexPath:fromIndexPath];
 }
我有一个coreDataStack类,负责所有的核心数据(创建一个Singleton),它如下所示:

#import "CoreDataStack.h"

@implementation CoreDataStack

#pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

+ (instancetype)defaultStack {

    static CoreDataStack *defaultStack;
    static dispatch_once_t onceTocken;
    dispatch_once (&onceTocken, ^{
        defaultStack = [[self alloc] init];
    });

    return defaultStack;
}


- (NSURL *)applicationDocumentsDirectory {
    // The directory the application uses to store the Core Data store file. This code uses a directory named "digitalCrown.Lister" in the application's documents directory.
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

- (NSManagedObjectModel *)managedObjectModel {
    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Lister" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    // Create the coordinator and store

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Lister.sqlite"];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        // Report any error we got.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        // Replace this 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.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}


- (NSManagedObjectContext *)managedObjectContext {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        NSError *error = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext 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.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}


@end
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    Target *targetToMove = [self.fetchedResultsController objectAtIndexPath:sourceIndexPath];
    if (destinationIndexPath.row == 0) {
        // to move to row 0, the new sortTime can be any value
        // that is  less than the sortTime of the Target currently at
        // row 0
        Target *targetAtRow0 = [self.fetchedResultsController objectAtIndexPath:destinationIndexPath];
        targetToMove.sortTime = [targetAtRow0.sortTime dateByAddingTimeInterval:-1];
    } else {
        // for any other row, set the sortTime to be half way 
        // between the sortTime for the row before the new position
        // and the sortTime for the row at the new position
        Target *targetBefore = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:(destinationIndexPath.row-1) inSection:destinationIndexPath.section]];
        Target *targetAfter = [self.fetchedResultsController objectAtIndexPath:destinationIndexPath];
        double lowerTimeInterval = [targetBefore.sortTime timeIntervalSinceReferenceDate];
        double upperTimeInterval = [targetAfter.sortTime timeIntervalSinceReferenceDate];
        double newInterval = (lowerTimeInterval + upperTimeInterval)/2;
        NSDate *newSortTime = [NSDate dateWithTimeIntervalSinceReferenceDate:newInterval];
        targetToMove.sortTime = newSortTime;
    }
    NSError *error;
    if (![self.managedObjectContext save:&error]) {
        NSLog(@"SaveContext: error %@, %@", error, [error userInfo]);
        abort();
    }
}
每当我向核心数据添加新对象时,我都会这样做:

- (void)insertTeget {

    CoreDataStack *stack = [CoreDataStack defaultStack];
    Target *target = [NSEntityDescription insertNewObjectForEntityForName:@"Target" inManagedObjectContext:stack.managedObjectContext];
    if (self.myTextView.text != nil) {
        target.body = self.myTextView.text;
        target.time = [NSDate date];
    }

    [stack saveContext];

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


    static NSString *cellIdentifier = @"StackTableViewCell";

    Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];

    StackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StackTableViewCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }

    cell.cellLabel.text = target.body;

    cell.cellLabel.font = [UIFont fontWithName:@"Candara-Bold" size:20];

    cell.showsReorderControl = YES;



    // Configure the cell...

    return cell;
}
在表视图中,当我提取数据时,我是这样做的:

- (void)insertTeget {

    CoreDataStack *stack = [CoreDataStack defaultStack];
    Target *target = [NSEntityDescription insertNewObjectForEntityForName:@"Target" inManagedObjectContext:stack.managedObjectContext];
    if (self.myTextView.text != nil) {
        target.body = self.myTextView.text;
        target.time = [NSDate date];
    }

    [stack saveContext];

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


    static NSString *cellIdentifier = @"StackTableViewCell";

    Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];

    StackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StackTableViewCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }

    cell.cellLabel.text = target.body;

    cell.cellLabel.font = [UIFont fontWithName:@"Candara-Bold" size:20];

    cell.showsReorderControl = YES;



    // Configure the cell...

    return cell;
}
这是表视图控制器类中的fetchresultconroller/fetch请求配置:

- (NSFetchRequest *)targetsFetchRequest {

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Target"];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    return fetchRequest;
}


- (NSFetchedResultsController *)fetchedResultController {

    if (_fetchedResultController != nil) {
        return _fetchedResultController;
    }

    CoreDataStack *stack = [CoreDataStack defaultStack];

    NSFetchRequest *fetchRequest = [self targetsFetchRequest];

    _fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:stack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

    _fetchedResultController.delegate = self;

    return _fetchedResultController;

}
现在我想检查订单是否被更改,所以我需要更改数据库中的订单

我该怎么做


谢谢

> P>我建议您考虑在实体中添加另一个属性<代码>目标< /代码>,或者称之为“代码>排序命令< /代码>”?p>

当您获取结果时,请考虑按<代码>排序命令> />代码>排序。

要实现此功能,需要在创建每个实体时设置
sortOrder
值。这应该是相对简单的。。。在创建实体之前,请完成
fetchedResults
计数,假设下一个
sortOrder
值将等于计数值加1

在tableview中移动行时,可以使用代码重新指定受影响单元格的
sortOrder
值,使之等于
indexPath.row
属性


保存后,新的排序顺序应保持不变。

根据我的评论,fetched results controller将每个对象的indexPath基于底层fetch的排序顺序—在您的例子中是
time
。如果您希望用户能够覆盖此选项,并对行进行重新排序,则将FRC更改为使用不同的排序顺序。向实体添加一个名为
sortTime
的新属性(日期类型),并修改
targetsFetchRequest
方法以使用
sortTime
作为排序键

无论何时添加新的
目标
,都要同时设置
sortTime
。在对行重新排序时,设置
sortTime
的值,以确保FRC将目标放在新的索引位置。大概是这样的:

#import "CoreDataStack.h"

@implementation CoreDataStack

#pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

+ (instancetype)defaultStack {

    static CoreDataStack *defaultStack;
    static dispatch_once_t onceTocken;
    dispatch_once (&onceTocken, ^{
        defaultStack = [[self alloc] init];
    });

    return defaultStack;
}


- (NSURL *)applicationDocumentsDirectory {
    // The directory the application uses to store the Core Data store file. This code uses a directory named "digitalCrown.Lister" in the application's documents directory.
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

- (NSManagedObjectModel *)managedObjectModel {
    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Lister" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    // Create the coordinator and store

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Lister.sqlite"];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        // Report any error we got.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        // Replace this 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.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}


- (NSManagedObjectContext *)managedObjectContext {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        NSError *error = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext 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.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}


@end
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    Target *targetToMove = [self.fetchedResultsController objectAtIndexPath:sourceIndexPath];
    if (destinationIndexPath.row == 0) {
        // to move to row 0, the new sortTime can be any value
        // that is  less than the sortTime of the Target currently at
        // row 0
        Target *targetAtRow0 = [self.fetchedResultsController objectAtIndexPath:destinationIndexPath];
        targetToMove.sortTime = [targetAtRow0.sortTime dateByAddingTimeInterval:-1];
    } else {
        // for any other row, set the sortTime to be half way 
        // between the sortTime for the row before the new position
        // and the sortTime for the row at the new position
        Target *targetBefore = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:(destinationIndexPath.row-1) inSection:destinationIndexPath.section]];
        Target *targetAfter = [self.fetchedResultsController objectAtIndexPath:destinationIndexPath];
        double lowerTimeInterval = [targetBefore.sortTime timeIntervalSinceReferenceDate];
        double upperTimeInterval = [targetAfter.sortTime timeIntervalSinceReferenceDate];
        double newInterval = (lowerTimeInterval + upperTimeInterval)/2;
        NSDate *newSortTime = [NSDate dateWithTimeIntervalSinceReferenceDate:newInterval];
        targetToMove.sortTime = newSortTime;
    }
    NSError *error;
    if (![self.managedObjectContext save:&error]) {
        NSLog(@"SaveContext: error %@, %@", error, [error userInfo]);
        abort();
    }
}

这将触发FRC委托方法来执行必要的表视图更新。

您在“获取结果”控制器配置中指定了什么排序顺序?如果您允许用户对行进行重新排序,您需要在实体上有一个属性(例如“index”或“sortOrder”),您可以修改该属性以反映所选的顺序,老实说,我真的是一个初学者,所以我不知道该怎么回答你的问题,所以我只是对我的问题做了更多的解释,也许会帮助你理解更多:)谢谢。您还可以在创建和配置fetchedResultsController的位置添加代码吗?好的,因此您的FRC配置为使用
time
属性进行排序。如果您希望允许用户覆盖此选项并移动行,则需要使用不同的排序顺序。(或者我假设您可以修改对象的
time
属性,使其成为正确的indexPath。例如,如果您从第6行移动到第3行,则需要
time
大于第2行的时间,但小于第3行的时间。)@pbasdf是否有可能使用代码进行回答?我不确定保存按时间排序的对象是否重要,但当我显示表视图时,重要的是按创建时间显示它,因为这是一个队列…除非用户希望手动更改顺序…我确实需要这里的帮助:/