iOS - NSInternalInconsistencyException'保存时的循环依赖'

时间:2014-02-10 19:41:08

标签: ios iphone facebook parsing

因为我还在学习,所以你必须对我很轻松,但是我遇到这个线程错误弹出一次,我点击了“登录Facebook”按钮。这个项目用于“Tinder”或“Hot or Not”克隆,我还处于起步阶段。如果有人能解释我的错误,我真的很感激......

2014-02-10 14:27:37.629 MatchedUp[38775:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Found a circular dependency when saving.'
*** First throw call stack:
(
    0   CoreFoundation                      0x028115e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x025948b6 objc_exception_throw + 44
    2   CoreFoundation                      0x028113bb +[NSException raise:format:] + 139
    3   MatchedUp                           0x00006417 +[PFObject(Private) collectDirtyChildren:children:files:seen:seenNew:] + 1174
    4   MatchedUp                           0x00006200 +[PFObject(Private) collectDirtyChildren:children:files:seen:seenNew:] + 639
    5   MatchedUp                           0x000064c5 +[PFObject(Private) collectDirtyChildren:children:files:seen:seenNew:] + 1348
    6   MatchedUp                           0x00006200 +[PFObject(Private) collectDirtyChildren:children:files:seen:seenNew:] + 639
    7   MatchedUp                           0x000065b5 +[PFObject(Private) collectDirtyChildren:children:files:] + 118
    8   MatchedUp                           0x00007385 +[PFObject(Private) deepSaveAsync:withSessionToken:] + 204
    9   MatchedUp                           0x0000890d -[PFObject(Private) saveChildrenAsync:] + 79
    10  MatchedUp                           0x0000ba48 -[PFObject(Private) saveAsync:] + 151
    11  MatchedUp                           0x0000b94d __30-[PFObject(Private) saveAsync]_block_invoke + 43
    12  MatchedUp                           0x0007d0c0 -[PFTaskQueue enqueue:] + 193
    13  MatchedUp                           0x0000b91b -[PFObject(Private) saveAsync] + 104
    14  MatchedUp                           0x0000dbf3 -[PFObject saveInBackgroundWithBlock:] + 34
    15  MatchedUp                           0x00004741 __45-[AGLoginViewController uploadPFFileToParse:]_block_invoke + 321
    16  MatchedUp                           0x0007c7b4 __53-[PFTask thenCallBackOnMainThreadWithBoolValueAsync:]_block_invoke + 98
    17  MatchedUp                           0x0007c537 __40-[PFTask thenCallBackOnMainThreadAsync:]_block_invoke_2 + 241
    18  libdispatch.dylib                   0x02dd57f8 _dispatch_call_block_and_release + 15
    19  libdispatch.dylib                   0x02dea4b0 _dispatch_client_callout + 14
    20  libdispatch.dylib                   0x02dd875e _dispatch_main_queue_callback_4CF + 340
    21  CoreFoundation                      0x02876a5e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
    22  CoreFoundation                      0x027b76bb __CFRunLoopRun + 1963
    23  CoreFoundation                      0x027b6ac3 CFRunLoopRunSpecific + 467
    24  CoreFoundation                      0x027b68db CFRunLoopRunInMode + 123
    25  GraphicsServices                    0x038889e2 GSEventRunModal + 192
    26  GraphicsServices                    0x03888809 GSEventRun + 104
    27  UIKit                               0x01302d3b UIApplicationMain + 1225
    28  MatchedUp                           0x000051ad main + 141
    29  libdyld.dylib                       0x0307c70d start + 1
    30  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

这是我的代码:

//
//  AGLoginViewController.m
//  MatchedUp
//
//  Created by AG on 1/30/14.
//  Copyright (c) 2014 Alex Gartenberg. All rights reserved.
//

#import "AGLoginViewController.h"

@interface AGLoginViewController ()

@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (strong, nonatomic) NSMutableData *imageData;

@end

@implementation AGLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.activityIndicator.hidden = YES;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark

- (IBAction)loginButtonPressed:(UIButton *)sender
{
    self.activityIndicator.hidden = NO;
    [self.activityIndicator startAnimating];


    NSArray *permissionsArray = @[@"user_about_me", @"user_interests", @"user_relationships", @"user_birthday", @"user_location", @"user_relationship_details"];

    [PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
        [self.activityIndicator stopAnimating];
        self.activityIndicator.hidden = YES;

        if (!user) {
            if (!error) {
                UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Login Error" message:@"Facebook Login was not successful" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                [alertview show];
            }
            else {
                UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Login Error" message:[error description] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alertview show];
            }
        }
        else{
            [self updateUserInformation];
            [self performSegueWithIdentifier:@"loginToTabBarSegue" sender:self];
        }
    }];
}

#pragma mark - Helper Method

-(void)updateUserInformation
{
    FBRequest *request = [FBRequest requestForMe];
    [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

        if (!error) {
            NSDictionary *userDictionary = (NSDictionary *)result;

            //create URL
            NSString *facebookID = userDictionary[@"id"];
            NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]];

            NSMutableDictionary *userProfile = [[NSMutableDictionary alloc] initWithCapacity:8];
            if (userDictionary[@"name"]){
                userProfile[kCCUserProfileNameKey] = userDictionary[@"name"];
            }
            if (userDictionary[@"first_name"]){
                userProfile[kCCUserProfileFirstNameKey] = userDictionary[@"first_name"];
            }
            if (userDictionary[@"location"][@"name"]){
                userProfile[kCCUserProfileLocationKey] = userDictionary[@"location"][@"name"];
            }
            if (userDictionary[@"gender"]){
                userProfile[kCCUserProfileGenderKey] = userDictionary[@"gender"];
            }
            if (userDictionary[@"birthday"]){
                userProfile[kCCUserProfileBirthdayKey] = userDictionary[@"birthday"];
            }
            if (userDictionary[@"interested_in"]){
                userProfile[kCCUserProfileInterestedInKey] = userDictionary[@"interested_in"];
            }
            if ([pictureURL absoluteString]) {
                userProfile[kCCUserProfilePictureURL] = [pictureURL absoluteString];
            }

            [[PFUser currentUser] setObject:userProfile forKey:@"profile"];
            [[PFUser currentUser] saveInBackground];

            [self requestImage];
        }
        else {
            NSLog(@"Error in Facebook Request %@", error);
        }
    }];
}


-(void)uploadPFFileToParse:(UIImage *)image
{
    NSData *imageData = UIImageJPEGRepresentation(image, 0.8);

    if (!imageData) {
        NSLog(@"image data was not found");
        return;
    }

    PFFile *photoFile = [PFFile fileWithData:imageData];

    [photoFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            PFObject *photo = [PFObject objectWithClassName:kCCPhotoClassKey];
            [photo setObject:[PFUser currentUser] forKey:kCCPhotoUserKey];
            [photo setObject:photo forKey:kCCPhotoPictureKey];
            [photo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                NSLog(@"Photo Saved Successfully");
            }];
        }
    }];
}

-(void)requestImage
{

    PFQuery *query = [PFQuery queryWithClassName:kCCPhotoClassKey];
    [query whereKey:kCCPhotoUserKey equalTo:[PFUser currentUser]];

    [query countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
        if (number ==0)
        {
            PFUser *user = [PFUser currentUser];
            self.imageData = [[NSMutableData alloc] init];

            NSURL *profilePictureURL = [NSURL URLWithString:user[kCCUserProfileKey][kCCUserProfilePictureURL]];
            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:profilePictureURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:4.0f];
            NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
            if (!urlConnection) {
                NSLog(@"Failed to Download Picture");
            }
        }
    }];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.imageData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    UIImage *profileImage = [UIImage imageWithData:self.imageData];
    [self uploadPFFileToParse:profileImage];
}

@end

1 个答案:

答案 0 :(得分:4)

在这一行:

[photo setObject:photo forKey:kCCPhotoPictureKey];

您将photo对象设置为引用自身。我猜这是一个错字,创建了循环引用,你真的想要:

[photo setObject:photoFile forKey:kCCPhotoPictureKey];