问题
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];}
I want to find all objects when we retrieve the objects from the first query itself. Please solve my problem
回答1:
There is a method you can use to include
properties which are pointer values. You cannot use the include
method with relations. What I do instead is use a Cloud Code function to aggregate the results I want into a JSON object and return that object.
See the fetchPostDetails
function in the following script.
https://github.com/brennanMKE/PostThings/blob/master/Parse/PostThings/cloud/main.js
It fetches items are relation objects such as tags and likes which happen to be User
objects which are relations to the Post
class. There are also comments which are referenced as a pointer back to the post from each comment. The fetchPostTags
and fetchPostLikes
methods show how to fetch those relations and populate the JSON object which is holding all of the results. You need to deploy those Cloud Code update and then access it as a function from the iOS side. The results will come back as an NSDictionary with values for posts, tags, likes and comments. The posts are an array of Post objects. The tags, likes and comments are NSDictionary objects which have the postId as the key to access the array of Parse objects.
This way one call to the function will get you want you need.
I've included some of the code below as a reference in case what is on GitHub changes.
// 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();
});
},
};
来源:https://stackoverflow.com/questions/17722435/how-to-get-the-pfrelation-objects-while-getting-the-pfquery-objects