Swift 4从Firebase数据库中检索数据

时间:2018-02-23 22:30:42

标签: swift firebase firebase-realtime-database swift4

func retPost2(){
runNBPOST()
////////////////////////////////////1


var pic = [String]()
var p = Post(userId: "", postId: "", postType: "", description: "", Region: "", City: "", District: "", Street: "", Area: 0, View: "", TotalPrice: 0, Pictures: pic, StreetWidth: 0, PropertyType: "")






////// for to retrive all post




print(retNBPOST())
runNBPOST()
let nbpost = retNBPOST()


for i in 1..<nbpost{


    postiiD = "Post(\(i))"


    self._REF_BASE.child("Post").child(postiiD).observeSingleEvent(of: .value, with: { (snapshot) in
        let value = snapshot.value as? NSDictionary
        let UserID2 = value?["UserID"] as? String ?? ""
        p.userId = UserID2
    })





    self._REF_BASE.child("Post").child("\("Post(\(i))")/Adress").observeSingleEvent(of: .value, with: { (snapshot) in

        let value = snapshot.value as? NSDictionary
        let Region = value?["Region"] as? String ?? ""
        p.Region = Region
        let City = value?["City"] as? String ?? ""
        p.City = City
        print(p.City)
        let District = value?["District"] as? String ?? ""
        p.District = District
        let Street = value?["Street"] as? String ?? ""
        p.Street = Street
        let AdditionalNumber = value?["AdditionalNumber"] as? String ?? ""
        p.AdditionalNumber = AdditionalNumber
        let PostalCode = value?["PostalCode"] as? String ?? ""
        p.PostalCode = PostalCode
        let BuldingNo = value?["BuldingNo"] as? String ?? ""
        p.BuldingNo = BuldingNo


    })

    self._REF_BASE.child("Post").child("\(postiiD)/PostInfo").observeSingleEvent(of: .value, with: { (snapshot) in
        let value = snapshot.value as? NSDictionary
        let postType = value?["PostType"] as? String ?? ""
        p.postType = postType
        print(p.postType)
        let DateOfpost = value?["DateOfpost"] as? String ?? ""
        p.dateOfpost = DateOfpost
        let EndDateOfPost = value?["EndDateOfPost"] as? String ?? ""
        p.endDateOfPost = EndDateOfPost
        let NbOfShare = value?["NbOfShare"] as? String ?? ""
        p.nbOfShare = Int(NbOfShare)!
        let NbOfViews = value?["NbOfViews"] as? String ?? ""
        p.nbOfViews = Int(NbOfViews)!
        let LastUpdate = value?["LastUpdate"] as? String ?? ""
        p.lastUpdate = LastUpdate
        let Description = value?["Description"] as? String ?? ""
        p.description = Description
        let Publisher = value?["Publisher"] as? String ?? ""
        p.publisher = Publisher
        let ContactTime = value?["ContactTime"] as? String ?? ""
        p.contactTime = ContactTime
        let Payment = value?["Payment"] as? String ?? ""
        p.payment = Payment
        let TotalPrice = value?["TotalPrice"] as? String ?? ""
        p.TotalPrice = Double(TotalPrice)!
        let NearBy = value?["NearBy"] as? String ?? ""
        p.NearBy = NearBy
        let StreetWidth = value?["StreetWidth"] as? String ?? ""
        p.StreetWidth = Double(StreetWidth)!
        //  let Discount = value?["Discount"] as? String ?? ""
        // p.Discount =


    })



    self._REF_BASE.child("Post").child("\(postiiD)/Property").observeSingleEvent(of: .value, with: { (snapshot) in
        let value = snapshot.value as? NSDictionary
        let Area = value?["Area"] as? String ?? ""
        p.Area = Double(Area)!
        print(p.Area)
        let View = value?["View"] as? String ?? ""
        p.View = View
        let FamilyOrSingle = value?["FamilyOrSingle"] as? String ?? ""
        p.FamilyOrSingle = FamilyOrSingle
        let Level = value?["Level"] as? String ?? ""
        p.Level = Int(Level)!
        let HouseAge = value?["HouseAge"] as? String ?? ""
        p.HouseAge = Int(HouseAge)!
        let LandType = value?["LandType"] as? String ?? ""
        p.LandType = LandType
        let MeterPrice = value?["MeterPrice"] as? String ?? ""
        p.MeterPrice = Double(MeterPrice)!
        let NbRoom = value?["NbRoom"] as? String ?? ""
        p.NbRoom = Int(NbRoom)!

        let NbGuestroom = value?["NbGuestroom"] as? String ?? ""
        p.NbGuestroom = Int(NbGuestroom)!
        let NbBathroom = value?["NbBathroom"] as? String ?? ""
        p.NbBathroom = Int(NbBathroom)!
        let NbBedroom = value?["NbBedroom"] as? String ?? ""
        p.NbBedroom = Int(NbBedroom)!
        let NbLivingroom = value?["NbLivingroom"] as? String ?? ""
        p.NbLivingroom = Int(NbLivingroom)!
        let NbKitchen = value?["NbKitchen"] as? String ?? ""
        p.NbKitchen = Int(NbKitchen)!
        let PropertyType = value?["PropertyType"] as? String ?? ""
        p.PropertyType = PropertyType
        let NbApartment = value?["NbApartment"] as? String ?? ""
        p.NbApartment = Int(NbApartment)!


    })


    // complet
    self._REF_BASE.child("Post").child("\(postiiD)/Amenities").observeSingleEvent(of: .value, with: { (snapshot) in
        let value = snapshot.value as? NSDictionary
        let LiftAvailability = value?["LiftAvailability"] as? String ?? ""
        let KitchenAvailability = value?["KitchenAvailability"] as? String ?? ""
        let FurnitureAvailability = value?["FurnitureAvailability"] as? String ?? ""
        let CarageAvailability = value?["CarageAvailability"] as? String ?? ""
        let SwimmingPoolAvailability = value?["SwimmingPoolAvailability"] as? String ?? ""
        let ParkingAvailability = value?["ParkingAvailability"] as? String
        let FiberOpticAvailability = value?["FiberOpticAvailability"] as? String
        let FireplaceAvailability = value?["FireplaceAvailability"] as? String
        let DiningroomAvailability = value?["DiningroomAvailability"] as? String
        let LaundryAvailability = value?["LaundryAvailability"] as? String
        let CentralAirAvailability = value?["CentralAirAvailability"] as? String
        let BalconyAvailability = value?["BalconyAvailability"] as? String
        let MaidRoomAvailability = value?["MaidRoomAvailability"] as? String
        let DriverRoomAvailability = value?["DriverRoomAvailability"] as? String
        let InternalStairAvailability = value?["InternalStairAvailability"] as? String
        let BasementAvailability = value?["BasementAvailability"] as? String
    })


    arrpost.append(p)


}


}


func updateHomeView(post : Post){

    totalPriceLb.text = "\(String(post.TotalPrice)) SR"
    areaLb.text = String(post.Area)
    AddressLb.text = "\(post.City) \(post.District) \(post.Street)"
    imageName.image = UIImage(named: "HomePic.jpg")

    if post.PropertyType == "Home" {
        bathLb.text = String(post.NbBathroom)
        BedLb.text = String(post.NbBedroom)
        imageName.image = UIImage(named: "HomePic.jpg")
    }else if post.PropertyType == "Apartment" {
        bathLb.text = String(post.NbBathroom)
        BedLb.text = String(post.NbBedroom)
        imageName.image = UIImage(named: "ApartPic.jpg")
    }else if post.PropertyType == "Land"{
        bathLb.isHidden = true
        BedLb.isHidden = true
        imageName.image = UIImage(named: "LandPic.jpg")
    }




}





func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {



    DataService.instance.retPost2()
  // tableView.reloadData()


  return DataService.instance.arrpost.count

}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)



}

func tableView
    (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



    let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! homecell

    let post = DataService.instance.arrpost[indexPath.row]
 //  let post = DataServiceTemp.instance.getPosts()[indexPath.row]
    cell.updateHomeView(post: post)

  // tableView.reloadData()

    return cell


}

我的IOS应用程序出了问题。我正在使用Firebase进行保存和维护检索数据。所有连接都很好,数据检索得很好或有时不完整。 而我的问题是当我运行应用程序时,视图,标签,图片等显示为空这个组件应该显示从firebase检索的数据。我不知道是什么问题。是时候还是其他什么?因此,主要问题是在完全检索数据之前组件显示为空。我想强制应用程序最初不显示为空,但显示带有数据的组件。

my Firebase Database

我已经使用了谷歌的所有方法 https://firebase.google.com/docs/database/ios/read-and-write

1 个答案:

答案 0 :(得分:0)

UIView悬停在使用UIActivityIndicator的mainVC上方,直到来自Firebase的响应将其删除