Ios 改变岩心数据中数据集顺序的尝试与磨难
我有一个Ios 改变岩心数据中数据集顺序的尝试与磨难,ios,objective-c,uitableview,core-data,Ios,Objective C,Uitableview,Core Data,我有一个ensorderedset附加到核心数据对象,该对象被传递到UITableViewController。managedObjectContext在UITableViewController中处于活动状态,并且MySensorDeredSet正确显示mytableView。当我转到reorderMySensorOrderedSet时,应用程序崩溃。我在moveRowatineXpath中放置了一个断点,以查看发生了什么,并且在尝试保存之前一切都正常 - (void)tableView:(U
ensorderedset
附加到核心数据对象,该对象被传递到UITableViewController
。managedObjectContext
在UITableViewController
中处于活动状态,并且MySensorDeredSet
正确显示mytableView
。当我转到reorderMySensorOrderedSet
时,应用程序崩溃。我在moveRowatineXpath
中放置了一个断点,以查看发生了什么,并且在尝试保存之前一切都正常
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
// BREAKPOINT: "po self.routineToEdit.exercises" shows order is 0, 1, 2
NSMutableOrderedSet *orderedSet = [self.routineToEdit.exercises mutableCopy];
[orderedSet exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
// BREAKPOINT: "po self.routineToEdit.exercises" shows order is 2, 1, 0
self.routineToEdit.exercises = orderedSet;
[self saveContext];
}
- (void)saveContext {
if (self.managedObjectContext != nil) {
NSError *error = nil;
if ([self.managedObjectContext hasChanges] && ![self.managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
我不想将我的对象传递到MyUITableViewController
,而是尝试使用fetchedResultsController
获取我的对象并将其显示在MyUITableViewController
上。在我走这条路线之前,我希望看到我可以使用从另一个合适的视图控制器传入的对象重新订购一个传感器数据集
以下是我的崩溃的控制台输出:
2015-07-12 06:39:53.176 MyApp[26505:1105589] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString appendString:]: nil argument'
*** First throw call stack:
(
0 CoreFoundation 0x00000001060dec65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000105663bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001060deb9d +[NSException raise:format:] + 205
3 CoreFoundation 0x00000001060afabf mutateError + 159
4 CoreData 0x000000010591ec26 -[_NSSQLGenerator prepareMasterReorderStatementPart2ForRelationship:] + 118
5 CoreData 0x000000010598cfb8 -[NSSQLAdapter newCorrelationMasterReorderStatementPart2ForRelationship:] + 72
6 CoreData 0x00000001059a5671 -[NSSQLiteConnection writeCorrelationMasterReordersFromTracker:] + 817
7 CoreData 0x00000001059a5f81 -[NSSQLiteConnection writeCorrelationChangesFromTracker:] + 65
8 CoreData 0x0000000105997627 -[NSSQLCore writeChanges] + 1351
9 CoreData 0x00000001058d32c7 -[NSSQLCore saveChanges:] + 423
10 CoreData 0x00000001058a3e74 -[NSSQLCore executeRequest:withContext:error:] + 484
11 CoreData 0x000000010597ee3f __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 4335
12 CoreData 0x0000000105987c30 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 192
13 libdispatch.dylib 0x0000000108890614 _dispatch_client_callout + 8
14 libdispatch.dylib 0x0000000108876002 _dispatch_barrier_sync_f_invoke + 365
15 CoreData 0x0000000105979245 _perform + 197
16 CoreData 0x00000001058a3a58 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 504
17 CoreData 0x00000001058cd52d -[NSManagedObjectContext save:] + 1213
18 MyApp 0x000000010511083e -[EditRoutineTableViewController saveContext] + 222
19 MyApp 0x0000000105110673 -[EditRoutineTableViewController tableView:moveRowAtIndexPath:toIndexPath:] + 371
20 UIKit 0x0000000106817822 -[UITableView _endReorderingForCell:wasCancelled:animated:] + 565
21 UIKit 0x000000010682b89d -[UIControl touchesEnded:withEvent:] + 462
22 UIKit 0x0000000106767958 -[UIWindow _sendTouchesForEvent:] + 735
23 UIKit 0x0000000106768282 -[UIWindow sendEvent:] + 682
24 UIKit 0x000000010672e541 -[UIApplication sendEvent:] + 246
25 UIKit 0x000000010673bcdc _UIApplicationHandleEventFromQueueEvent + 18265
26 UIKit 0x000000010671659c _UIApplicationHandleEventQueue + 2066
27 CoreFoundation 0x0000000106012431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
28 CoreFoundation 0x00000001060082fd __CFRunLoopDoSources0 + 269
29 CoreFoundation 0x0000000106007934 __CFRunLoopRun + 868
30 CoreFoundation 0x0000000106007366 CFRunLoopRunSpecific + 470
31 GraphicsServices 0x000000010a12ba3e GSEventRunModal + 161
32 UIKit 0x00000001067198c0 UIApplicationMain + 1282
33 MyApp 0x000000010510a05f main + 111
34 libdyld.dylib 0x00000001088c4145 start + 1
谢谢你的阅读。我欢迎你的意见
我发现这个问题与我遇到的问题处于“同一个邻域”,但是我的集合的顺序并不像示例中给出的那样是静态的
我认为fetch results controller在这里没有给您任何优势(主要是因为排序要求在这里没有意义)
我将直接使用有序集(正在编辑的对象的对多关系)来通知表视图数据源。我会检查将新排序的集合分配回对象是否按预期工作 StackOverflow的共识似乎是,核心数据如何处理
传感器数据集
关系存在缺陷。我能够使用Dmitry Makarenko关于Xcode生成的访问器的帖子中包含的信息来操纵我的传感器数据集的顺序:
我还清理了(至少我认为是这样)我在UITableView
中用于移动行的代码:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSMutableOrderedSet *orderedSet = [self.routineToEdit mutableOrderedSetValueForKey:@"stretches"].mutableCopy;
// This is necessary, as exchangeObjectAtIndex:withObjectAtIndex doesn't work passing in indexPath.row
NSInteger fromIndex = fromIndexPath.row;
NSInteger toIndex = toIndexPath.row;
// Swap objects at indexes
[orderedSet exchangeObjectAtIndex:fromIndex withObjectAtIndex:toIndex];
NSLog(@"ending orderedSet = /n%@", orderedSet);
// Assign NSMutableOrderdSet to the object's NSOrderedSet
self.routineToEdit.stretches = orderedSet;
// Added performBlockAndWait so the save is complete before doing anything else
[self.persistentStoreCoordinator performBlockAndWait:^{
[self saveContext];
}];
}
在交换UITableView行/NSObject之后立即将对象保存到managedObjectContext
,这一点非常关键——当我单击“完成iAction
w/此处调用的保存方法”时,我发现并没有保存所有更改:
[self.persistentStoreCoordinator performBlockAndWait:^{
[self saveContext];
}];
…但应用程序仍然崩溃
最后,我将这两个实体之间的关系更改为ordered,这就成功了:
就我而言,这已经足够了。我独立地尝试了每一种修复方法,但没有一种能够独立工作。在这一点上,我的应用程序按照我希望的方式工作,所以我现在就不去管它了。通过调用mutableOrderedSetValueForKey:
,你已经成功了一半,不要调用mutableCopy
!另外,不要将此可变顺序集重新分配回练习
。只需保存您的上下文,在上下文中调用refreshObject:mergeChanges:
,并且练习
应反映您的更改。感谢您帮助我排除另一种方法。在我保存到managedObjectContext
之前,我仍然不明白为什么一切看起来都像预期的那样运行。在这一点上,我打赌我是一个方法和/或一行或两行代码短,使这项工作。谢谢你的输入!这是我没有尝试(也应该尝试)的东西。我现在可以使用它了,但在将其保存到managedObjectContext
时遇到了问题。当我在交换索引后键入po self.routineToEdit.exercises
时,它打印了我所期望的内容。我是否应该在[self.moc save:&error]
之前刷新对象:合并更改:
?刷新对象:合并更改:
应在保存后调用。它告诉对象从存储重新加载新值。谢谢。我把它加入了我今天早上拼凑的解决方案中。