Firebase,检索数据:找到所有具有“状态”的标题。 as' Open'在斯威夫特

时间:2016-07-05 19:05:38

标签: swift firebase firebase-realtime-database

主要任务是找到所有具有“状态”的标题。 as'打开'

我想把所有的favit都放到一个要在indexPath.row中使用的数组

func retrieveQuery(){
    var i = 0
    queryRef.queryOrderedByChild("status").queryEqualToValue("Open")
        .observeEventType(.ChildAdded, withBlock:
        { snapshot in 
            i++
            self.antal = snapshot.children.allObjects.count
            print(self.antal)
            print(snapshot.value.objectForKey("favorTit")!)
            print(i)                
        })  
}

Firebase数据 https://api.myjson.com/bins/4n5rn

{
    "categories": [{
        "altText": "T.ex. Städning, Disk, Tvätt, Matlagning",
        "title": "Hushåll"
    }, {
        "altText": "T.ex.  ",
        "title": " "
    }, {
        "altText": " ",
        "title": " "
    }, {
        "altText": " ",
        "title": " "
    }, {
        "altText": " ",
        "title": " "
    }, {
        "altText": " ",
        "title": " "
    }, {
        "altText": " ",
        "title": " "
    }],
    "favors": {
        "1": {
            "catId": 1,
            "favorBudget": 212121,
            "favorDes": "Gfdgsfdsg",
            "favorDueDate": "Today",
            "favorGeo": [-7.090910999999999, 107.668887],
            "favorLocation": ["West Java, Indonesia"],
            "favorTit": "Rätt",
            "status": "Open",
            "user": "2872e074-e45a-4d7a-a9c7-83fad641aa62",
            "workCompletion": "In person"
        },
        "2": {
            "catId": 1,
            "favorBudget": 4000,
            "favorDes": "Gfdgf",
            "favorDueDate": "Today",
            "favorGeo": [34.506668, -81.948334],
            "favorLocation": ["Laurens County Airport, Laurens, SC 29360, USA"],
            "favorTit": "Rätt",
            "status": "Open",
            "user": "2872e074-e45a-4d7a-a9c7-83fad641aa62",
            "workCompletion": "In person"
        },
        "3": {
            "catId": 1,
            "favorBudget": 4000,
            "favorDes": "Gfdgf",
            "favorDueDate": "Today",
            "favorGeo": [34.506668, -81.948334],
            "favorLocation": ["Laurens County Airport, Laurens, SC 29360, USA"],
            "favorTit": "Rätt",
            "status": "Ongoing",
            "user": "2872e074-e45a-4d7a-a9c7-83fad641aa62",
            "workCompletion": "In person"
        },
        "7fd547be-7836-42e2-a74f-2f2a39baee43": {
            "favorTit": "Test",
            "favors": {
                "furniture assmebly": {
                    "favorBudget": 60000,
                    "favorDes": "Assemly My ikea",
                    "favorDueDate": "Today",
                    "favorGeo": [39.0311755, -77.5283463],
                    "favorLocation": ["Polen Farm Blvd, Ashburn, VA 20148, USA"],
                    "favorTit": "Den ska bli fixad kom snabbt",
                    "workCompletion": "In person"
                },
                "handyman": {
                    "favorBudget": 43434,
                    "favorDes": "Install TV-Mount",
                    "favorDueDate": "Today",
                    "favorGeo": [49.0068901, 8.4036527],
                    "favorLocation": ["Karlsruhe, Germany"],
                    "favorTit": "JAllah",
                    "workCompletion": "In person"
                },
                "photography": {
                    "favorBudget": 6000,
                    "favorDes": "Jag vill ha ett album med bilder på mig och omgivningen💪 Du ska inte säga mycket under bröllopet men det ska vara vackra bilder",
                    "favorDueDate": "Within a week",
                    "favorGeo": [55.6178043, 12.98939],
                    "favorLocation": ["Krankajen 36, 211 12 Malmö, Sverige"],
                    "favorTit": "take a photo of my wedding",
                    "workCompletion": "In person"
                },
                "trädgård": {
                    "favorBudget": 2000,
                    "favorDes": "Jag vill ha den klippt med en sax",
                    "favorDueDate": "Within a week",
                    "favorGeo": [35.86166, 104.195397],
                    "favorLocation": ["China"],
                    "favorTit": "Klipp min gräsmatta",
                    "workCompletion": "In person"
                }
            },
            "status": "Done"
        }
    },
    "users": {
        "2872e074-e45a-4d7a-a9c7-83fad641aa62": {
            "email": "fille382@gmail.com",
            "name": "lol",
            "phone": "123567890",
            "provider": "password"
        },
        "5f0fb39e-620a-4cd1-9e6a-2b7ae9baaf71": {
            "email": "lol@gmail.com",
            "name": "Gunn Bellander",
            "phone": "0735158548",
            "provider": "password"
        },
        "7fd547be-7836-42e2-a74f-2f2a39baee43": {
            "about": "Johan my name is Filip Bellander and i am creating this beautiful app right now the about page is here so the user can quickly reach its target by writing about himself and his completions ",
            "comments": {
                "4354352": {
                    "comment": "He did everything even cleaned up after himself",
                    "stars": 3,
                    "tasktitle": "Do everything"
                },
                "423489054": {
                    "comment": "Yes very nice",
                    "stars": 1,
                    "tasktitle": "call my phone"
                },
                "5435486956": {
                    "comment": "It was very clean",
                    "stars": 3,
                    "tasktitle": "Clean room"
                },
                "54643654654": {
                    "comment": "He did a great job wiping all whipcream from the luggage",
                    "stars": 4,
                    "tasktitle": "Whipe dat"
                }
            },
            "completed": 90,
            "email": "test@gmail.com",
            "name": "Filip Bellander",
            "phone": "0735158548",
            "posted": 81,
            "provider": "password",
            "rating": 5,
            "reviews": 1337,
            "skills": "App programmer, Visual designer DriversDriverslicenseDriverslicenseDriverslicenseDriverslicenselicense no"
        },
        "bdc6c3f8-764a-4468-825a-408f53695b24": {
            "email": "Elisabet.bellander@gmail.com",
            "name": "Elisabet Bellander",
            "phone": "0721645504",
            "provider": "password"
        }
    }
}

2 个答案:

答案 0 :(得分:0)

从您的代码中,您应该已经在快照中拥有Open Titles数组。

if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
    self.items = result
}

然后您的TableView将取决于您的实施方式。有一些示例herehere

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  ...
  let cellDict = items[indexPath.row]
  ...
  return cell
}

答案 1 :(得分:0)

我可以通过使用下面的代码在每次运行时在索引0处插入字符串来创建数组

queryRef.queryOrderedByChild("status").queryEqualToValue("Open")
        .observeEventType(.ChildAdded, withBlock:
        { snapshot in

            self.antal = snapshot.children.allObjects.count
            print(self.antal)

            var titles = snapshot.value.objectForKey("favorTit") as! String
            var description = snapshot.value.objectForKey("favorDes") as! String
            var budget = snapshot.value.objectForKey("favorBudget") as! Double

            self.openTitles.insert(titles, atIndex: 0)
            self.openDescription.insert(description, atIndex: 0)
            self.openBudget.insert(budget, atIndex: 0)

            print(self.openTitles)
            print(self.openDescription)


        })