未解决的错误错误域= NSCocoaErrorDomain代码= 132001“(空)”

时间:2019-09-23 02:50:08

标签: ios swift xcode core-data

当我升级到iOS 13时,我的应用程序中的许多东西似乎都坏了。其中之一是如何保存我的数据。当我点击“保存”按钮时,出现此未解决的错误:

    Unresolved error Error Domain=NSCocoaErrorDomain Code=132001 "(null)" UserInfo={message=attempt to recursively call -save: on the context aborted, stack trace=(
    0   CoreData                            0x00007fff23759236 -[NSManagedObjectContext save:] + 170
    1   owed                                0x000000010f86f76a $s4owed13CoreDataStackC11saveContextyyF + 186
    2   owed                                0x000000010f8d8347 $s4owed14ViewControllerC05tableB0_12cellForRowAtSo07UITableB4CellCSo0iB0C_10Foundation9IndexPathVtF + 10215
    3   owed                                0x000000010f8d8cb5 $s4owed14ViewControllerC05tableB0_12cellForRowAtSo07UITableB4CellCSo0iB0C_10Foundation9IndexPathVtFTo + 165
    4   UIKitCore                           0x00007fff47360e4b -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 787
    5   UIKitCore                           0x00007fff47361364 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 73
    6   UIKitCore                           0x00007fff4738ea9c -[_UITableViewUpdateSupport(Private) _setupAnimationsForNewlyInsertedCells] + 7750
    7   UIKitCore                           0x00007fff4739805f -[_UITableViewUpdateSupport _setupAnimations] + 118
    8   UIKitCore                           0x00007fff4732efb0 -[UITableView _updateWithItems:updateSupport:] + 2883
    9   UIKitCore                           0x00007fff47326f5f -[UITableView _endCellAnimationsWithContext:] + 17188
    10  UIKitCore                           0x00007fff47340d22 -[UITableView endUpdatesWithContext:] + 112
    11  owed                                0x000000010f8db9fb $s4owed14ViewControllerC26controllerDidChangeContentyySo016NSFetchedResultsC0CySo20NSFetchRequestResult_pGF + 267
    12  owed                                0x000000010f8dba64 $s4owed14ViewControllerC26controllerDidChangeContentyySo016NSFetchedResultsC0CySo20NSFetchRequestResult_pGFTo + 68
    13  CoreData                            0x00007fff238e00be __82-[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:]_block_invoke + 7591
    14  CoreData                            0x00007fff2376ebfb developerSubmittedBlockToNSManagedObjectContextPerform + 154
    15  CoreData                            0x00007fff2376eae2 -[NSManagedObjectContext performBlockAndWait:] + 197
    16  CoreData                            0x00007fff238de305 -[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:] + 105
    17  CoreFoundation                      0x00007fff23ac2c1c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    18  CoreFoundation                      0x00007fff23ac20a5 _CFXRegistrationPost1 + 421
    19  CoreFoundation                      0x00007fff23ac1e0f ___CFXNotificationPost_block_invoke + 175
    20  CoreFoundation                      0x00007fff23bbffe3 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1811
    21  CoreFoundation                      0x00007fff23ac1776 _CFXNotificationPost + 950
    22  Foundation                          0x00007fff2564d429 -[NSNotificationCenter postNotificationName:object:userInfo:] + 59
    23  CoreData                            0x00007fff2375b4fe -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 541
    24  CoreData                            0x00007fff237feb3a -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:deletions:updates:refreshes:deferrals:wasMerge:] + 1557
    25  CoreData                            0x00007fff23755ff3 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 1217
    26  CoreData                            0x00007fff237592fb -[NSManagedObjectContext save:] + 367
    27  owed                                0x000000010f86f76a $s4owed13CoreDataStackC11saveContextyyF + 186
    28  owed                                0x000000010f955d53 $s4owed23AddPersonViewControllerC8saveNameyyypF + 11699
    29  owed                                0x000000010f956360 $s4owed23AddPersonViewControllerC8saveNameyyypFTo + 80
    30  UIKitCore                           0x00007fff47163d19 -[UIApplication sendAction:to:from:forEvent:] + 83
    31  UIKitCore                           0x00007fff46b55599 -[UIControl sendAction:to:forEvent:] + 223
    32  UIKitCore                           0x00007fff46b558e3 -[UIControl _sendActionsForEvents:withEvent:] + 398
    33  UIKitCore                           0x00007fff46b54842 -[UIControl touchesEnded:withEvent:] + 481
    34  UIKitCore                           0x00007fff4719e21f -[UIWindow _sendTouchesForEvent:] + 2604
    35  UIKitCore                           0x00007fff4719fb26 -[UIWindow sendEvent:] + 4596
    36  UIKitCore                           0x00007fff4717b1a7 -[UIApplication sendEvent:] + 356
    37  UIKitCore                           0x00007fff471faa18 __dispatchPreprocessedEventFromEventQueue + 6847
    38  UIKitCore                           0x00007fff471fd4de __handleEventQueueInternal + 5980
    39  CoreFoundation                      0x00007fff23afbac1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    40  CoreFoundation                      0x00007fff23afb9ec __CFRunLoopDoSource0 + 76
    41  CoreFoundation                      0x00007fff23afb1c4 __CFRunLoopDoSources0 + 180
    42  CoreFoundation                      0x00007fff23af5ecf __CFRunLoopRun + 1263
    43  CoreFoundation                      0x00007fff23af56b6 CFRunLoopRunSpecific + 438
    44  GraphicsServices                    0x00007fff3815cbb0 GSEventRunModal + 65
    45  UIKitCore                           0x00007fff47162a67 UIApplicationMain + 1621
    46  owed                                0x000000010f8fe4db main + 75
    47  libdyld.dylib                       0x00007fff5123bcf5 start + 1
)}, ["stack trace": <_NSCallStackArray 0x600000e5d560>(
0   CoreData                            0x00007fff23759236 -[NSManagedObjectContext save:] + 170,
1   owed                                0x000000010f86f76a $s4owed13CoreDataStackC11saveContextyyF + 186,
2   owed                                0x000000010f8d8347 $s4owed14ViewControllerC05tableB0_12cellForRowAtSo07UITableB4CellCSo0iB0C_10Foundation9IndexPathVtF + 10215,
3   owed                                0x000000010f8d8cb5 $s4owed14ViewControllerC05tableB0_12cellForRowAtSo07UITableB4CellCSo0iB0C_10Foundation9IndexPathVtFTo + 165,
4   UIKitCore                           0x00007fff47360e4b -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 787,
5   UIKitCore                           0x00007fff47361364 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 73,
6   UIKitCore                           0x00007fff4738ea9c -[_UITableViewUpdateSupport(Private) _setupAnimationsForNewlyInsertedCells] + 7750,
7   UIKitCore                           0x00007fff4739805f -[_UITableViewUpdateSupport _setupAnimations] + 118,
8   UIKitCore                           0x00007fff4732efb0 -[UITableView _updateWithItems:updateSupport:] + 2883,
9   UIKitCore                           0x00007fff47326f5f -[UITableView _endCellAnimationsWithContext:] + 17188,
10  UIKitCore                           0x00007fff47340d22 -[UITableView endUpdatesWithContext:] + 112,
11  owed                                0x000000010f8db9fb $s4owed14ViewControllerC26controllerDidChangeContentyySo016NSFetchedResultsC0CySo20NSFetchRequestResult_pGF + 267,
12  owed                                0x000000010f8dba64 $s4owed14ViewControllerC26controllerDidChangeContentyySo016NSFetchedResultsC0CySo20NSFetchRequestResult_pGFTo + 68,
13  CoreData                            0x00007fff238e00be __82-[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:]_block_invoke + 7591,
14  CoreData                            0x00007fff2376ebfb developerSubmittedBlockToNSManagedObjectContextPerform + 154,
15  CoreData                            0x00007fff2376eae2 -[NSManagedObjectContext performBlockAndWait:] + 197,
16  CoreData                            0x00007fff238de305 -[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:] + 105,
17  CoreFoundation                      0x00007fff23ac2c1c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12,
18  CoreFoundation                      0x00007fff23ac20a5 _CFXRegistrationPost1 + 421,
19  CoreFoundation                      0x00007fff23ac1e0f ___CFXNotificationPost_block_invoke + 175,
20  CoreFoundation                      0x00007fff23bbffe3 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1811,
21  CoreFoundation                      0x00007fff23ac1776 _CFXNotificationPost + 950,
22  Foundation                          0x00007fff2564d429 -[NSNotificationCenter postNotificationName:object:userInfo:] + 59,
23  CoreData                            0x00007fff2375b4fe -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 541,
24  CoreData                            0x00007fff237feb3a -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:deletions:updates:refreshes:deferrals:wasMerge:] + 1557,
25  CoreData                            0x00007fff23755ff3 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 1217,
26  CoreData                            0x00007fff237592fb -[NSManagedObjectContext save:] + 367,
27  owed                                0x000000010f86f76a $s4owed13CoreDataStackC11saveContextyyF + 186,
28  owed                                0x000000010f955d53 $s4owed23AddPersonViewControllerC8saveNameyyypF + 11699,
29  owed                                0x000000010f956360 $s4owed23AddPersonViewControllerC8saveNameyyypFTo + 80,
30  UIKitCore                           0x00007fff47163d19 -[UIApplication sendAction:to:from:forEvent:] + 83,
31  UIKitCore                           0x00007fff46b55599 -[UIControl sendAction:to:forEvent:] + 223,
32  UIKitCore                           0x00007fff46b558e3 -[UIControl _sendActionsForEvents:withEvent:] + 398,
33  UIKitCore                           0x00007fff46b54842 -[UIControl touchesEnded:withEvent:] + 481,
34  UIKitCore                           0x00007fff4719e21f -[UIWindow _sendTouchesForEvent:] + 2604,
35  UIKitCore                           0x00007fff4719fb26 -[UIWindow sendEvent:] + 4596,
36  UIKitCore                           0x00007fff4717b1a7 -[UIApplication sendEvent:] + 356,
37  UIKitCore                           0x00007fff471faa18 __dispatchPreprocessedEventFromEventQueue + 6847,
38  UIKitCore                           0x00007fff471fd4de __handleEventQueueInternal + 5980,
39  CoreFoundation                      0x00007fff23afbac1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17,
40  CoreFoundation                      0x00007fff23afb9ec __CFRunLoopDoSource0 + 76,
41  CoreFoundation                      0x00007fff23afb1c4 __CFRunLoopDoSources0 + 180,
42  CoreFoundation                      0x00007fff23af5ecf __CFRunLoopRun + 1263,
43  CoreFoundation                      0x00007fff23af56b6 CFRunLoopRunSpecific + 438,
44  GraphicsServices                    0x00007fff3815cbb0 GSEventRunModal + 65,
45  UIKitCore                           0x00007fff47162a67 UIApplicationMain + 1621,
46  owed                                0x000000010f8fe4db main + 75,
47  libdyld.dylib                       0x00007fff5123bcf5 start + 1
)
, "message": attempt to recursively call -save: on the context aborted]

这是保存功能:

 /* =====================================
     Save Function
     ======================================*/
    @IBAction func saveName(_ sender: Any) {

        /* STEP 1 - Check to see if there is a person name in the input field */
        guard name.text! != "", name.text! != "Add Name" else {
            // call the warning function
            warningText = "You forgot to add a name."
            warningMessage()
            return
        }

        /* STEP 2 - Check to see if that persons names was already used */
        let personFetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
        personFetchRequest.predicate = NSPredicate(format: "name == %@", name.text!)

        print(name.text!)

        do{
            let results = try coreDataStack.managedContext.fetch(personFetchRequest)
            print("\(results.count)  this is the results count for the predicate")
            guard results.count == 0 else{
                // print("The Same Name is Used")
                warningText = "You already used that name."
                warningMessage()
                return
            }

            /* STEP 3 - Use the name that was typed in */
            let person = Person(entity: Person.entity(), insertInto: coreDataStack.managedContext)
            let trimmedName = name.text!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
            person.name = trimmedName

            if(email.text! == "" || email.text! == "Email Address"){
                person.email = "Email Address"
            }else{
               person.email = email.text
            }

            if(mobile.text == "" || mobile.text == "Mobile Number"){
                person.mobile = "Mobile Number"
            }
            else{
                person.mobile = mobile.text
            }

            person.dateCreated = NSDate() as Date
            person.priority = 9999

            /* STEP 4 - Save the data */
            coreDataStack.saveContext()
            self.dismiss(animated: true, completion: {});

        }catch{
            print("Error")
        }

        self.dissmissViewController(cancelBtn!)
    }

当我关闭应用程序并将其打开时,将显示正确的数据。我不确定该错误对我说了什么,以弄清楚从哪里开始研究答案。

1 个答案:

答案 0 :(得分:1)

要在没有更多代码的情况下准确地查看正在发生的事情有点困难,但似乎是在告诉您您要递归保存上下文(即出于某种原因,保存上下文的行为导致保存了上下文,这导致上下文被保存,导致上下文被保存。...等)

从调用堆栈看来,正在发生以下情况:

  1. 您保存上下文
  2. 您使用的FetchedResultsController会收到有关更改的通知。
  3. 您的UITableView使用新数据进行更新
  4. 显示新单元格的操作以某种方式触发了上下文的再次保存。 这就是问题所在-这不应该发生。
  5. 您正在使用的FetchedResultsController会收到有关某些更改的通知。
  6. 第3-5步永远重复

我会在调用coreDataStack.saveContext()的任何地方放置断点,运行应用程序,然后尝试查看为什么保存后显示单元格时递归调用它。