如何获取PFquery对象时获取PFRelation对象?

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"(UserId==%@)",[defaluts objectForKey:@"objectId"]]; PFQuery *frndquery=[PFQuery queryWithClassName:@"FriendsDetails" predicate:predicate]; [frndquery orderByDescending:@"lastdate"]; [frndquery whereKey:@"BlockStatus" equalTo:@"No"]; NSArray *arrquery=[frndquery findObjects]; for (PFObject *frndids in arr){ PFRelation *relation=[frndids relationforKey:@"ChatRelation"]; NSArray *arrids=[NSArray arrayWithObjects:[frndids objectForKey:@"UserId"],[frndids objectForKey:@"ConversationID"], nil]; PFQuery *statusQuery = [relation query]; [statusQuery orderByDescending:@"createdAt"]; [statusQuery whereKey:@"Deletechat" notContainedIn:arrids]; statusQuery.limit = [[NSNumber numberWithInt:1] intValue]; NSArray *arrforrelationobjects=[statusQuery findObjects];} 

当我们从第一个查询本身检索对象时,我想查找所有对象。 请解决我的问题

有一种方法可以用来include指针值的属性。 include关系的方法不能使用。 我所做的是使用云代码函数将我想要的结果聚合到JSON对象中并返回该对象。

请参阅以下脚本中的fetchPostDetails函数。

https://github.com/brennanMKE/PostThings/blob/master/Parse/PostThings/cloud/main.js

它提取项目是关系对象,如标签和喜欢,恰好是与Post类的关系的User对象。 也有评论被引用作为从每个评论回到post的指针。 fetchPostTagsfetchPostLikes方法显示如何获取这些关系,并填充持有所有结果的JSON对象。 您需要部署这些云代码更新,然后从iOS端作为函数进行访问。 结果将返回作为一个NSDictionary与职位,标签,喜欢和评论的值。 post是Post对象的数组。 tags,likes和comments是NSDictionary对象,它们的postId是访问Parse对象数组的关键。

这样一个函数的调用会让你想要你的需要。

我已经在下面的代码中包含了一些代码,以防GitHub发生了什么变化。

 // Helper functions in PT namespace var PT = { eachItem : function (items, callback) { var index = 0; var promise = new Parse.Promise(); var continueWhile = function(nextItemFunction, asyncFunction) { var item = nextItemFunction(); if (item) { asyncFunction(item).then(function() { continueWhile(nextItemFunction, asyncFunction); }); } else { promise.resolve(); } }; var nextItem = function() { if (index < items.length) { var item = items[index]; index++; return item; } else { return null; } }; continueWhile(nextItem, callback); return promise; }, arrayContainsItem : function(array, item) { // True if item is in array var i = array.length; while (i--) { if (array[i] === item) { return true; } } return false; }, arrayContainsOtherArray : function(array, otherArray) { /// True if each item in other array is in array var i = otherArray.length; while (i--) { if (!PT.arrayContainsItem(array, otherArray[i])) { return false; } } return true; }, fetchPostTags : function(post) { return post.relation("tags").query().find(); }, fetchPostLikes : function(post) { return post.relation("likes").query().find(); }, fetchPostComments : function(post) { var query = new Parse.Query(Comment); query.include("owner"); query.equalTo("post", post); return query.find(); }, fetchPostDetails : function(post, json) { json.tags[post.id] = []; json.likes[post.id] = []; json.comments[post.id] = []; return PT.fetchPostTags(post).then(function(tags) { json.tags[post.id] = tags; return PT.fetchPostLikes(post); }).then(function(likes) { json.likes[post.id] = likes; return PT.fetchPostComments(post); }).then(function(comments) { json.comments[post.id] = comments; json.count++; return Parse.Promise.as(); }); }, };