我有一个NSOrderedSet
附加到传递到中的Core Data对象UITableViewController
。该managedObjectContext
是活得很好的UITableViewController
和myNSOrderedSet
正确显示我的tableView
。当我重新订购时myNSOrderedSet
,应用程序崩溃。我设置了一个断点,moveRowAtIndexPath
以查看发生了什么,并且一切正常,直到尝试保存它为止。
- (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
我不想将我的对象传递进去,而是用afetchedResultsController
来抓住我的对象并在上面显示它MyUITableViewController
。在走那条路线之前,我想看看我可以NSOrderedSet
使用从传入的对象重新排序AnotherUITableViewController
。
这是我崩溃时的控制台输出:
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
感谢您的阅读。欢迎您的投入。
我发现此问题位于我遇到的问题的“相同邻域”中,但是我的集合的顺序不是像示例中给出的示例那样是静态的。
StackOverflow共识似乎在NSOrderedSet
Core Data处理关系方面存在一个错误。我能够NSOrderedSet
使用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];
}];
}
managedObjectContext
交换UITableView行/ NSObject之后,将对象保存到右侧是至关重要的-当我单击在其中IBAction
调用了save方法的“完成”时,看到并非所有更改都被保存时,我发现了这一点:
[self.persistentStoreCoordinator performBlockAndWait:^{
[self saveContext];
}];
...但是该应用仍然崩溃。
最终,我将两个实体之间的关系更改为有序,并且达到了目的:
就我的目的而言,这就足够了。我独立尝试了这些修复程序,但没有一个可以独立运行。在这一点上,我的应用程序可以按照我想要的方式工作,因此现在暂时不做介绍。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句