简体   繁体   中英

Get map data from Cloud Firestore

I want to get map data from cloud Firestore ,this is java code I need to access the same database in swift. I attached the java code and database structure picture for reference.

FirebaseFirestore db = FirebaseFirestore.getInstance();
    DocumentReference mDocRef = db.collection("hmdhAcademy").document("notifications");

    mDocRef.get().addOnCompleteListener(task -> {

        if ( task.isSuccessful() ) {

            DocumentSnapshot documentSnapshot = task.getResult();

            if ( documentSnapshot != null && documentSnapshot.exists() ){

                notificationList = (ArrayList) documentSnapshot.get("userNotifications");
                adapter = new NotificationAdapter(this,notificationList);
                mRecyclerView.setAdapter(adapter);
                alertDialog.dismiss();
            }
        }
        else {

            Toast.makeText(NotificationActivity.this,"Check Internet Connection",Toast.LENGTH_SHORT).show();
            alertDialog.dismiss();
        }

    });

if ( list.get(i) instanceof  HashMap ){

        final Intent intent = new Intent(mContext,NotificationDetail.class);

        String title = (String)((HashMap)list.get((list.size()-1)-i)).get("title");
        String body = (String)((HashMap)list.get((list.size()-1)-i)).get("body");
        String image = (String)((HashMap)list.get((list.size()-1)-i)).get("notiImage");
        String detail = (String)((HashMap)list.get((list.size()-1)-i)).get("detail");
}

在此处输入图片说明

I need the same title, body, notiImage and detail.

Please help me with that, I'm looking for an answer anywhere but failed.

Try this code:

docRef.getDocument { (document, error) in
if let document = document, document.exists {
    print(document.data()!)
    let userNotifications = document.data()["userNotifications"] as? [[String:Any]]
    for notificaton in userNotifications  {
        let body = notificaton["body"] as? String ?? ""
        let title = notificaton["title"] as? String ?? ""
        print(body, title)

    }

} else {
    print("Document does not exist")
}
}

Or try this way:

  1. Create a struct

     struct usernotifcatons { var body: String = "" var detail: String = "" var notiImage: String = "" var title: String = "" init(notificationData: [String:Any]) { let body = notificationData["body"] as? String ?? "" self.body = body let detail = notificationData["detail"] as? String ?? "" self.detail = detail let notiImage = notificationData["notiImage"] as? String ?? "" self.notiImage = notiImage let title = notificationData["title"] as? String ?? "" self.title = title } } 
  2. Create a variable in your ViewController:

     var allNotifications: [usernotifcatons] = [usernotifcatons]() 
  3. Map all your data in one line in your firestore code

     docRef.getDocument { (document, error) in if let document = document, document.exists { print(document.data()!) let userNotifications = document.data()?["userNotifications"] as? [[String:Any]] for data in userNotifications! { self.allNotifications.append(usernotifications(notificationData: data)) } print(self.allNotifications) } else { print("Document does not exist") } } 

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