【问题标题】:Parse incrementKey making iOS app to crash解析 incrementKey 使 iOS 应用程序崩溃
【发布时间】:2020-01-19 18:36:22
【问题描述】:

我试图在我的 Parse 数据库中将名为“ThumbsDown”的列增加 1,但是我执行这种递增的代码行使应用程序崩溃。该列的类型为“数字”。

PFQuery *downvoteUser = [PFQuery queryWithClassName:@"UserRating"];
[downvoteUser whereKey:@"User" equalTo:PFUser.currentUser.username];

     NSArray *downVote = [downvoteUser findObjects];

     NSLog(@"objectID : %@", [downVote valueForKey:@"objectId"]);
     NSLog(@"ThumbsDown : %@", [downVote valueForKey:@"ThumbsDown"]);

     // Here we update the Rated column in Event Class
     PFObject *CurrentUserThumbsDown = [PFObject objectWithoutDataWithClassName:@"UserRating" objectId:[downVote valueForKey:@"objectId"]];
     [CurrentUserThumbsDown incrementKey:@"ThumbsDown"];

     [CurrentUserThumbsDown save];

根据我的测试:

  • 如果我删除incrementKey,它不会崩溃;但它不会做任何事情
  • NSLogs 实际上返回预期值,从云中获取。
  • 我无法从保存中打印出错误,因为它在捕捉到它之前就崩溃了

我得到的崩溃错误是这个:

2020-01-19 19:25:41.024887+0100 Sporteve[39724:418054] Warning: A long-running operation is being executed on the main thread. 
Break on warnBlockingOperationOnMainThread() to debug.
2020-01-19 19:25:41.025422+0100 Sporteve[39724:418243] -[__NSSingleObjectArrayI length]: unrecognized selector sent to instance 0x600001572ef0
2020-01-19 19:25:41.027731+0100 Sporteve[39724:418243] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectArrayI length]: unrecognized selector sent to instance 0x600001572ef0'

编辑/提示:

  • 如果我将 PFObject 中的 objectID 更改为数据库中的真实 objectId,它实际上可以工作,所以问题一定出在 NSArray 中。

【问题讨论】:

  • 您期望有多少对象匹配查询?

标签: ios objective-c parse-platform increment


【解决方案1】:

超级简单。

@Vadian 问了我正确的问题。我只检索 1 个对象,所以正确的行是这一行:

PFObject *CurrentUserThumbsDown = [PFObject objectWithoutDataWithClassName:@"UserRating" objectId:[downVote[0] valueForKey:@"objectId"]];

编辑:

下面的 cmets 中提供的一个更好的想法是只 getFirstObject,这是一个更好的想法:

PFObject *CurrentUserThumbsDown = [downvoteUser getFirstObject];

【讨论】:

  • 更好的答案是 getFirstObjectInBackground 有两个原因:(1) 查询应该异步完成 (2) getFirst 检索单个对象,当搜索的键是唯一的时,这是您所期望的
  • 为什么不使用 API 返回一个(第一个)对象?并且不要使用valueForKey,除非您知道 KVC 是什么并且您需要它。使用 objectForKey 或 - 可使用 10 年 - 密钥订阅 (downVote[@"ThumbsDown"])
猜你喜欢
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
相关资源
最近更新 更多