"Original" use Bmob as iOS background development experience--Query Association relationship (Bmobrelation)

Source: Internet
Author: User

The data association in Bmob is divided into two kinds of pointer and relation (the Data Association is used in the process of developing the app or is very frequent, also is a difficulty.) Although I have not studied the database before, but feel that it is necessary to familiarize and master it , Bmob official documents have been introduced in detail.

Basically looking at the document will solve most of the problems associated with the data (Bmob's documentation really likes it). Here I record one of the most difficult parts of my understanding at the time-- query correlation .

Or use the official documentation of the case to learn (this site in some parts of the Objectid written wrong, recorded the time of 2015-09-14).

Post table (all user published articles are placed in the Post table content refers to the article contents, title refers to the article title, author is the pointer type, point to the _user table)

_user Table (username represents user name, password represents user password)

Our task is to query all users like the post table in Objectid for a397e97451 This article, simply to find objectid for a397e97451 articles All user of the Association (Relation) in likes.

    //Association Object TableBmobquery *bquery = [Bmobquery querywithclassname:@"_user"]; //columns that need to be queriedBmobobject *post = [Bmobobject objectwithoutdatatwithclassname:@"Post"ObjectId:@"a397e97451"]; [Bquery Whereobjectkey:@"likes"Relatedto:post]; [Bquery Findobjectsinbackgroundwithblock:^ (Nsarray *array, Nserror *error) {        if(Error) {NSLog (@"%@", error); } Else {             for(Bmobobject *userinchArray) {NSLog (@"%@", [user objectforkey:@"username"]); }        }    }];

The query code given by the official website above. But I really did not understand this query way at first, bquery obvious and "likes" and post is not a road. Until I saw the following phrase:

Unlike pointer, the nature of relation queries here is the _user table.

So what does the above code actually do?

1. Because we are looking for all the user who likes this article, we first find all the user in the _user table.

2. But the user we found in the previous step did not like this article, so we have to have a screening method. And this screening method is whereobjectkey:relatedto:, Relatedto Point is that I want to query the article, and Whereobjectkey point is the article corresponding to the user association relationship. As to why to add Whereobjectkey, it is because there may be multiple relationships in the post table that relate to user, such as the user who may be involved in the post, so reviews the article (associated with user relationships). we can understand that in the previous step, all the user in the _user table is stored in the bquery, and the likes in Post is the Objectid (or a uniform identifier such as a pointer) that prefers the article user. Then traverse all the user, find the corresponding Objectid equal user, access down, as a result of the search. (Above is purely speculation!!!) )

1Nsmutablearray *searchresult =Initialize;2  for(Bmobuser *userinch_user all user in table)3 {4     if(User.objectid equals one of the objectId in the likes of the Post)5     {6 [SearchResult Adduser:user];7     }8 }9 returnSearchResult;//The user that you want to query is stored.

Summarize

Original "Using Bmob as iOS background development experience--Query Association relationship (bmobrelation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.