将对象添加到Parse.com类时出现异常

时间:2014-04-23 19:47:11

标签: ios objective-c macos parse-platform

我在向Parse创建和发送记录时遇到以下异常,我想知道是否有人可以阐明它,或者如何寻找解决方案..

2014-04-23 22:38:19.397 DreamCatching[10731:303] Can't use nil for keys or values on PFObject. Use NSNull for values.
2014-04-23 22:38:19.399 DreamCatching[10731:303] (
    0   CoreFoundation                      0x00007fff8933a25c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff89827e75 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8933a10c +[NSException raise:format:] + 204
    3   ParseOSX                            0x00000001000cc9ac -[PFObject setObject:forKey:] + 69
    4   DreamCatching                       0x000000010000681f -[DreamViewController endPlaces:] + 975
    5   AppKit                              0x00007fff8dfbd260 -[NSApplication sendAction:to:from:] + 327
    6   AppKit                              0x00007fff8dfbd0de -[NSControl sendAction:to:] + 86
    7   AppKit                              0x00007fff8e009c4d -[NSCell _sendActionFrom:] + 128
    8   AppKit                              0x00007fff8e023655 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2316
    9   AppKit                              0x00007fff8e022a27 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 487
    10  AppKit                              0x00007fff8e02213d -[NSControl mouseDown:] + 706
    11  AppKit                              0x00007fff8dfa3a58 -[NSWindow sendEvent:] + 11296
    12  AppKit                              0x00007fff8df425d4 -[NSApplication sendEvent:] + 2021
    13  AppKit                              0x00007fff8dd92a19 -[NSApplication run] + 646
    14  AppKit                              0x00007fff8dd7d7a3 NSApplicationMain + 940
    15  DreamCatching                       0x000000010001a862 main + 34
    16  libdyld.dylib                       0x00007fff886a45fd start + 1
    17  ???                                 0x0000000000000003 0x0 + 3
)

我发送的记录看起来像这样(在崩溃之前记录):

2014-04-23 22:38:19.397 DreamCatching[10731:303] endPlaces: newJoin: <DDPlaceJoin:new:(null)> {
    ACL = "<PFACL: 0x6100002574f0>";
    mainKey = 070213681717;
    mainName = "About Time";
    parseUser = "<PFUser:BJdxGyXnkn>";
    placeKey = 042314094048;
    placeName = London;
}

Parse对象中没有其他用户元素。这种情况也只是偶尔发生,尽管经常发生。

我的代码如下:

- (IBAction)endPlaces:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    //selectedPlaces should be in its final state.
    //use it to rebuild the placesJoin class

    [self endSheet:self.sheet returnCode:result];

    //rebuild Places Join array and save it. The replacement info is in selectedPlaces

    //1. empty the current join array for this dream

    [self clearPlaceJoins];

    [PFQuery clearAllCachedResults];

    // 2. Build new join table based on selectedPlaces

    //NSLog(@"There are some Places noted in this dream");
    NSLog(@"endPlaces: selectedPlaces is %@", selectedPlaces);
    NSLog(@"endPlaces: selectedPlaces Count is %lu", (unsigned long)selectedPlaces.count);

    for (int i=0; i < selectedPlaces.count; i++) {

        PFObject *newJoin = [PFObject objectWithClassName:@"DDPlaceJoin"];
        NSString *tempDreamKey = [selectedPlaces[i] valueForKey:@"mainKey"];
        NSString *tempPlaceKey = [selectedPlaces[i] valueForKey:@"placeKey"];
        NSString *tempDreamName = [selectedPlaces[i] valueForKey:@"mainName"];
        NSString *tempPlaceName = [selectedPlaces[i] valueForKey:@"placeName"];

        [newJoin setObject:tempDreamKey forKey:@"mainKey"];
        [newJoin setObject:tempPlaceKey forKey:@"placeKey"];
        [newJoin setObject:tempDreamName forKey:@"mainName"];
        [newJoin setObject:tempPlaceName forKey:@"placeName"];
        [newJoin setObject:[PFUser currentUser] forKey:@"parseUser"];

        query.cachePolicy = kPFCachePolicyNetworkElseCache;

        //Set ACL permissions for added security
        PFACL *joinACL = [PFACL ACLWithUser:[PFUser currentUser]];
        [newJoin setACL:joinACL];

        NSLog(@"endPlaces: newJoin: %@", newJoin);

        [newJoin saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                // Revert state on save error.
                NSLog(@"Error saving a placejoin");
            }
        }];

    }

}

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

错误消息非常具有描述性,newJoin上设置的对象之一为零。也许是[PFUser currentUser]

每次调用setObject都可以像这样处理它...

[newJoin setObject:tempDreamKey ?: [NSNull null] forKey:@"mainKey"];

答案 1 :(得分:1)

setObject:forKey:的一次调用中发生崩溃,这意味着您的NSLog在崩溃之后。您可能会看到最后一个有效的记录,而不是导致问题的记录。

尝试在第一个setObject:forKey:之前放置一个断点或NSLog,并确保所有temp*值都是非零的。如果它们是零并且它们永远不应该是,那么你可能想弄明白为什么。否则,您应该确保在将这些值添加到[NSNull null]之前注意这些值并将其更改为PFObject