如何在获取PFquery对象的同时获取PFRelation对象? [英] How to get the PFRelation objects while getting the PFquery objects?

查看:47
本文介绍了如何在获取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];} 

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

I want to find all objects when we retrieve the objects from the first query itself. Please solve my problem

推荐答案

有一种方法可以用来 include 属性(它们是指针值).您不能将 include 方法用于关联.我要做的是使用Cloud Code函数将我想要的结果聚合到JSON对象中并返回该对象.

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.

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

See the fetchPostDetails function in the following script.

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

它获取项目是关系对象,例如标签和点赞,它们恰好是与 Post 类有关系的 User 对象.也有一些评论,它们被引用为每个评论中指向该帖子的指针. fetchPostTags fetchPostLikes 方法显示了如何获取这些关系以及如何填充保存所有结果的JSON对象.您需要部署这些Cloud Code更新,然后从iOS端作为功能访问它.结果将以NSDictionary的形式返回,其中包含帖子,标签,喜欢和评论的值.这些帖子是Post对象的数组.标签,喜欢和注释是NSDictionary对象,它们具有postId作为访问Parse对象数组的键.

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.

为了防止GitHub上的内容发生变化,我提供了以下一些代码作为参考.

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();
        });
    },

};

这篇关于如何在获取PFquery对象的同时获取PFRelation对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆