简体   繁体   中英

How to get a query retrieving two objects from Parse via Swift

I have a parse database as in the picture.

在此处输入图片说明

I need to retrieve with a query code, two objects, for example the last created dates of type 2 and 3.

I am trying the codes below, but dont know how to merge these two queries (query2 and query3)? Or might there be another way to retrieve these two objects as one table?

    var query2 = PFQuery(className: "stories")
    query2.whereKey("Type", equalTo: 2)
    query2.addDescendingOrder("createdAt")
    query2.getFirstObject()

    var query3 = PFQuery(className: "stories")
    query3.whereKey("Type", equalTo: 3)
    query3.addDescendingOrder("createdAt")
    query3.getFirstObject()

I don't think you could do exactly what you currently have with 1 query. You could combine them but only to get back an array of all 2 and 3 types and then split them out yourself.

If the issue is making 2 different network requests then you could create a cloud code function to run the 2 queries and return the results in a single response.

you can do this one query as well

var query2 = PFQuery(className: "stories")
query2.whereKey("Type", equalTo: 2)
query2.whereKey("Type", equalTo: 3)
query2.addDescendingOrder("createdAt")

You can change you approach too , you can send array to compare and sort results

query2.whereKey("Type", containedIn: @[2,3])
query2.addDescendingOrder("createdAt")

After this don't use getFirstObject ; get all array and get your desired results using predicate or any else , this will save one network call as well.

This is for getting 2 values from Array

NSArray *fullArray= All objects from the parse ;

NSMutableArray *selectedObjectsArray = [NSMutableArray array];

for(int i=0 ; i<fullArray.count ; i++){
    if (selectedObjectsArray.count==1 && ![[[selectedObjectsArray objectAtIndex:0] objcetForKey:@"type"] isEqualToString:[[Full array objectAtIndex:i] objcetForKey:@"type"]]) {
        [selectedObjectsArray addObject:[fullArray objectAtIndex]];
        break ;
    }else{
        [selectedObjectsArray addObject:[fullArray objectAtIndex]];
    }
}

In Swift (im not too good in swift so double check swift code before using)

for object in fullArray {
    if (selectedObjectsArray.count==1 && ![[selectedObjectsArray[0][@"type"] isEqualToString:fullArray[i][@"type"]) {
        selectedObjectsArray[1]=[fullArray[i];
        break ;
    }else{
        selectedObjectsArray[0]=[fullArray[i];
    }

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM