0

I am continuously getting a SIGTRAP error from this function

- (void)saveShindyToDatabase
{

    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    // url = [url URLByAppendingPathComponent:@"Default Shindy Database"];
    self.shindyDatabase = [[UIManagedDocument alloc] initWithFileURL:url];

    [self.shindyDatabase setValue:self.detailView.text forKey:@"details"];

    if (FBSession.activeSession.isOpen) {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
             if (!error) {
                 self.name = user.name;
                 self.photo.profileID = user.id;
                 self.username = user.username;
             }
         }];
    }

    // [Guest guestWithName:self.name username:self.username photo:self.photo inManagedObjectContext:self.shindyDatabase.managedObjectContext];
    [self.shindyDatabase setValue:self.locationManager.location forKey:@"location"];
    [self.shindyDatabase setValue:self.dateAndTimePicker.date forKey:@"dateAndTime"];
}

I'm trying to execute the function when the user taps a button, or in the background. Either one returns the same error.

What am I doing wrong?

4

1 回答 1

0

弄清楚了。我正在调用主线程。

- (void)saveShindyToDatabase
{

    dispatch_queue_t fetchQ = dispatch_queue_create("HomeViewController", nil);
    dispatch_async(fetchQ, ^{
        NSArray *shindys = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
        [self.shindyDatabase.managedObjectContext performBlock:^{
            for (NSDictionary *shindyInfo in shindys) {
                [Shindy shindyWithShindyDBInfo:shindyInfo inManagedObjectContext:self.shindyDatabase.managedObjectContext];

                NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
                // url = [url URLByAppendingPathComponent:@"Default Shindy Database"];
                self.shindyDatabase = [[UIManagedDocument alloc] initWithFileURL:url];

                [self.shindyDatabase setValue:self.detailView.text forKey:@"details"];

                if (FBSession.activeSession.isOpen) {
                    [[FBRequest requestForMe] startWithCompletionHandler:
                     ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                         if (!error) {
                             self.name = user.name;
                             self.photo.profileID = user.id;
                             self.username = user.username;
                         }
                     }];
                }

                // [Guest guestWithName:self.name username:self.username photo:self.photo inManagedObjectContext:self.shindyDatabase.managedObjectContext];
                [self.shindyDatabase setValue:self.locationManager.location forKey:@"location"];
                [self.shindyDatabase setValue:self.dateAndTimePicker.date forKey:@"dateAndTime"];
            }
        }];
    });
}
于 2012-12-18T22:30:18.103 回答