swift firestore tinder - fetch users and add to an array to load on screen - Advanced












0















I am making a tinder type app, at the moment I have cardViewModels: [CardViewModel] which contains 4 fictitious app users in the producers array of user objects.
this currently works fine and loads the user on to the tinder type cards.



though now I would like to some how replace the fictitious users with other fictitious users from Firebase/Firestore.



I would expect to call fetchUsersCurrentUserIsFollowing() which returns
UserGrapevine users (as seen below)



UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)


I then wish to append these users to the producers array and then call setupDummyCards() after my producers array has been set with the users from Firestore?



    let cardViewModels: [CardViewModel] = {

let producers = [
UserTinder(name: "Kelly", age: 23, profession: "Music DJ", imageNames: ["lady5c","lady4c","kelly1","kelly2","kelly3"]),
UserTinder(name: "Jane", age: 18, profession: "Teacher", imageNames: ["jane1","jane2","jane3"]),
Advertiser(title: "Buy My Shirt", brandName: "Ben's Shirts", posterPhotoName: "lady5c"), Joker(title: "JOKER", cardSuit: "HEART", jokerPhotoName: "lady5c")] as [ProducesCardViewModel]


let viewModels = producers.map({return $0.toCardViewModel()})


return viewModels

}()


fileprivate func setupDummyCards() {
cardViewModels.forEach { (cardVM) in
let cardView = CardView(frame: .zero)
cardView.cardViewModel = cardVM
cardsDeckView.addSubview(cardView)
cardView.fillSuperview()
}
}




func fetchUsersCurrentUserIsFollowing(){

guard let uid = Auth.auth().currentUser?.uid else { return }

let db = Firestore.firestore()

db.collection("Users").document(uid).collection("Following").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: (err)")
} else {
for document in querySnapshot!.documents {
//print("(document.documentID) => (document.data())")

let d = document.data()

d.forEach({ (key: String, value: Any) in
print("this is the key",key)


Database.firestorefetchUserForGrapevineCardWithUID(uid: key, completion: { (user) in


//append to producers list somehow.

}


)}
)}
}
}
}






extension Database {
static func firestorefetchUserForGrapevineCardWithUID(uid: String, completion:@escaping (UserGrapevine) -> ()) {

print("fire store fetch user with uid called")

let defaults = UserDefaults.standard


let db = Firestore.firestore()

let docRef = db.collection("Users").document(uid)

docRef.getDocument { (document, error) in

if (document?.exists)! {

if let document = document {

guard let dictionary = document.data() else {
print("error 1245")
return }
guard let age = dictionary["Age"] as? Int else { return }


guard let occupation = dictionary["Occupation"] as? String else {
return }

guard let displayName = dictionary["Display Name"] as? String else {
return }

guard let searchingFromLatitude = defaults.string(forKey: "SearchingFromLatitude")
else {
print("error 1234324245")
return }

guard let searchingFromLongitude = defaults.string(forKey: "SearchingFromLongitude") else {
print("error 1832835")
return }

guard let DoubleLatitude = Double(searchingFromLatitude) else {
print("error t345345345")
return }
guard let DoubleLongitude = Double(searchingFromLongitude) else {
print("error 55545345")
return }

//////////////Start of distanceFrom code///////////////////
guard let latitude = dictionary["Actual Latitude"] as? String, let longitude = dictionary["Actual Longitude"] as? String, let latDouble = Double(latitude), let longDouble = Double(longitude) else {
return }

let distanceInKms = Database.getDistanceInKms(currentUserLat: DoubleLatitude, currentUserLong: DoubleLongitude, lat: latDouble, long: longDouble)
///////////////End of distance code/////////////////



let user = UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)



completion(user)


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

}
}









share|improve this question

























  • So whats the issue? your question reads more like a task requirement. are you getting some errors in your code? unexpected results?

    – Scriptable
    Nov 14 '18 at 11:24











  • //append to producers list somehow. have you tried to loop through the users and add each user to the list? producers.append(user) or producers += users

    – Scriptable
    Nov 14 '18 at 11:26











  • Scriptable , yes this is where i am having trouble, how do i access the producers array to append the users from inside the func fetchUsersCurrentUserIsFollowing() ? and then once they are all appended have the screen load?

    – robbieboy888
    Nov 14 '18 at 11:37













  • Value of type 'NewGrapevineSwipeController' has no member 'producers'

    – robbieboy888
    Nov 14 '18 at 11:39











  • as its inside of let cardViewModels: [CardViewModel] =

    – robbieboy888
    Nov 14 '18 at 11:40
















0















I am making a tinder type app, at the moment I have cardViewModels: [CardViewModel] which contains 4 fictitious app users in the producers array of user objects.
this currently works fine and loads the user on to the tinder type cards.



though now I would like to some how replace the fictitious users with other fictitious users from Firebase/Firestore.



I would expect to call fetchUsersCurrentUserIsFollowing() which returns
UserGrapevine users (as seen below)



UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)


I then wish to append these users to the producers array and then call setupDummyCards() after my producers array has been set with the users from Firestore?



    let cardViewModels: [CardViewModel] = {

let producers = [
UserTinder(name: "Kelly", age: 23, profession: "Music DJ", imageNames: ["lady5c","lady4c","kelly1","kelly2","kelly3"]),
UserTinder(name: "Jane", age: 18, profession: "Teacher", imageNames: ["jane1","jane2","jane3"]),
Advertiser(title: "Buy My Shirt", brandName: "Ben's Shirts", posterPhotoName: "lady5c"), Joker(title: "JOKER", cardSuit: "HEART", jokerPhotoName: "lady5c")] as [ProducesCardViewModel]


let viewModels = producers.map({return $0.toCardViewModel()})


return viewModels

}()


fileprivate func setupDummyCards() {
cardViewModels.forEach { (cardVM) in
let cardView = CardView(frame: .zero)
cardView.cardViewModel = cardVM
cardsDeckView.addSubview(cardView)
cardView.fillSuperview()
}
}




func fetchUsersCurrentUserIsFollowing(){

guard let uid = Auth.auth().currentUser?.uid else { return }

let db = Firestore.firestore()

db.collection("Users").document(uid).collection("Following").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: (err)")
} else {
for document in querySnapshot!.documents {
//print("(document.documentID) => (document.data())")

let d = document.data()

d.forEach({ (key: String, value: Any) in
print("this is the key",key)


Database.firestorefetchUserForGrapevineCardWithUID(uid: key, completion: { (user) in


//append to producers list somehow.

}


)}
)}
}
}
}






extension Database {
static func firestorefetchUserForGrapevineCardWithUID(uid: String, completion:@escaping (UserGrapevine) -> ()) {

print("fire store fetch user with uid called")

let defaults = UserDefaults.standard


let db = Firestore.firestore()

let docRef = db.collection("Users").document(uid)

docRef.getDocument { (document, error) in

if (document?.exists)! {

if let document = document {

guard let dictionary = document.data() else {
print("error 1245")
return }
guard let age = dictionary["Age"] as? Int else { return }


guard let occupation = dictionary["Occupation"] as? String else {
return }

guard let displayName = dictionary["Display Name"] as? String else {
return }

guard let searchingFromLatitude = defaults.string(forKey: "SearchingFromLatitude")
else {
print("error 1234324245")
return }

guard let searchingFromLongitude = defaults.string(forKey: "SearchingFromLongitude") else {
print("error 1832835")
return }

guard let DoubleLatitude = Double(searchingFromLatitude) else {
print("error t345345345")
return }
guard let DoubleLongitude = Double(searchingFromLongitude) else {
print("error 55545345")
return }

//////////////Start of distanceFrom code///////////////////
guard let latitude = dictionary["Actual Latitude"] as? String, let longitude = dictionary["Actual Longitude"] as? String, let latDouble = Double(latitude), let longDouble = Double(longitude) else {
return }

let distanceInKms = Database.getDistanceInKms(currentUserLat: DoubleLatitude, currentUserLong: DoubleLongitude, lat: latDouble, long: longDouble)
///////////////End of distance code/////////////////



let user = UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)



completion(user)


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

}
}









share|improve this question

























  • So whats the issue? your question reads more like a task requirement. are you getting some errors in your code? unexpected results?

    – Scriptable
    Nov 14 '18 at 11:24











  • //append to producers list somehow. have you tried to loop through the users and add each user to the list? producers.append(user) or producers += users

    – Scriptable
    Nov 14 '18 at 11:26











  • Scriptable , yes this is where i am having trouble, how do i access the producers array to append the users from inside the func fetchUsersCurrentUserIsFollowing() ? and then once they are all appended have the screen load?

    – robbieboy888
    Nov 14 '18 at 11:37













  • Value of type 'NewGrapevineSwipeController' has no member 'producers'

    – robbieboy888
    Nov 14 '18 at 11:39











  • as its inside of let cardViewModels: [CardViewModel] =

    – robbieboy888
    Nov 14 '18 at 11:40














0












0








0








I am making a tinder type app, at the moment I have cardViewModels: [CardViewModel] which contains 4 fictitious app users in the producers array of user objects.
this currently works fine and loads the user on to the tinder type cards.



though now I would like to some how replace the fictitious users with other fictitious users from Firebase/Firestore.



I would expect to call fetchUsersCurrentUserIsFollowing() which returns
UserGrapevine users (as seen below)



UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)


I then wish to append these users to the producers array and then call setupDummyCards() after my producers array has been set with the users from Firestore?



    let cardViewModels: [CardViewModel] = {

let producers = [
UserTinder(name: "Kelly", age: 23, profession: "Music DJ", imageNames: ["lady5c","lady4c","kelly1","kelly2","kelly3"]),
UserTinder(name: "Jane", age: 18, profession: "Teacher", imageNames: ["jane1","jane2","jane3"]),
Advertiser(title: "Buy My Shirt", brandName: "Ben's Shirts", posterPhotoName: "lady5c"), Joker(title: "JOKER", cardSuit: "HEART", jokerPhotoName: "lady5c")] as [ProducesCardViewModel]


let viewModels = producers.map({return $0.toCardViewModel()})


return viewModels

}()


fileprivate func setupDummyCards() {
cardViewModels.forEach { (cardVM) in
let cardView = CardView(frame: .zero)
cardView.cardViewModel = cardVM
cardsDeckView.addSubview(cardView)
cardView.fillSuperview()
}
}




func fetchUsersCurrentUserIsFollowing(){

guard let uid = Auth.auth().currentUser?.uid else { return }

let db = Firestore.firestore()

db.collection("Users").document(uid).collection("Following").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: (err)")
} else {
for document in querySnapshot!.documents {
//print("(document.documentID) => (document.data())")

let d = document.data()

d.forEach({ (key: String, value: Any) in
print("this is the key",key)


Database.firestorefetchUserForGrapevineCardWithUID(uid: key, completion: { (user) in


//append to producers list somehow.

}


)}
)}
}
}
}






extension Database {
static func firestorefetchUserForGrapevineCardWithUID(uid: String, completion:@escaping (UserGrapevine) -> ()) {

print("fire store fetch user with uid called")

let defaults = UserDefaults.standard


let db = Firestore.firestore()

let docRef = db.collection("Users").document(uid)

docRef.getDocument { (document, error) in

if (document?.exists)! {

if let document = document {

guard let dictionary = document.data() else {
print("error 1245")
return }
guard let age = dictionary["Age"] as? Int else { return }


guard let occupation = dictionary["Occupation"] as? String else {
return }

guard let displayName = dictionary["Display Name"] as? String else {
return }

guard let searchingFromLatitude = defaults.string(forKey: "SearchingFromLatitude")
else {
print("error 1234324245")
return }

guard let searchingFromLongitude = defaults.string(forKey: "SearchingFromLongitude") else {
print("error 1832835")
return }

guard let DoubleLatitude = Double(searchingFromLatitude) else {
print("error t345345345")
return }
guard let DoubleLongitude = Double(searchingFromLongitude) else {
print("error 55545345")
return }

//////////////Start of distanceFrom code///////////////////
guard let latitude = dictionary["Actual Latitude"] as? String, let longitude = dictionary["Actual Longitude"] as? String, let latDouble = Double(latitude), let longDouble = Double(longitude) else {
return }

let distanceInKms = Database.getDistanceInKms(currentUserLat: DoubleLatitude, currentUserLong: DoubleLongitude, lat: latDouble, long: longDouble)
///////////////End of distance code/////////////////



let user = UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)



completion(user)


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

}
}









share|improve this question
















I am making a tinder type app, at the moment I have cardViewModels: [CardViewModel] which contains 4 fictitious app users in the producers array of user objects.
this currently works fine and loads the user on to the tinder type cards.



though now I would like to some how replace the fictitious users with other fictitious users from Firebase/Firestore.



I would expect to call fetchUsersCurrentUserIsFollowing() which returns
UserGrapevine users (as seen below)



UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)


I then wish to append these users to the producers array and then call setupDummyCards() after my producers array has been set with the users from Firestore?



    let cardViewModels: [CardViewModel] = {

let producers = [
UserTinder(name: "Kelly", age: 23, profession: "Music DJ", imageNames: ["lady5c","lady4c","kelly1","kelly2","kelly3"]),
UserTinder(name: "Jane", age: 18, profession: "Teacher", imageNames: ["jane1","jane2","jane3"]),
Advertiser(title: "Buy My Shirt", brandName: "Ben's Shirts", posterPhotoName: "lady5c"), Joker(title: "JOKER", cardSuit: "HEART", jokerPhotoName: "lady5c")] as [ProducesCardViewModel]


let viewModels = producers.map({return $0.toCardViewModel()})


return viewModels

}()


fileprivate func setupDummyCards() {
cardViewModels.forEach { (cardVM) in
let cardView = CardView(frame: .zero)
cardView.cardViewModel = cardVM
cardsDeckView.addSubview(cardView)
cardView.fillSuperview()
}
}




func fetchUsersCurrentUserIsFollowing(){

guard let uid = Auth.auth().currentUser?.uid else { return }

let db = Firestore.firestore()

db.collection("Users").document(uid).collection("Following").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: (err)")
} else {
for document in querySnapshot!.documents {
//print("(document.documentID) => (document.data())")

let d = document.data()

d.forEach({ (key: String, value: Any) in
print("this is the key",key)


Database.firestorefetchUserForGrapevineCardWithUID(uid: key, completion: { (user) in


//append to producers list somehow.

}


)}
)}
}
}
}






extension Database {
static func firestorefetchUserForGrapevineCardWithUID(uid: String, completion:@escaping (UserGrapevine) -> ()) {

print("fire store fetch user with uid called")

let defaults = UserDefaults.standard


let db = Firestore.firestore()

let docRef = db.collection("Users").document(uid)

docRef.getDocument { (document, error) in

if (document?.exists)! {

if let document = document {

guard let dictionary = document.data() else {
print("error 1245")
return }
guard let age = dictionary["Age"] as? Int else { return }


guard let occupation = dictionary["Occupation"] as? String else {
return }

guard let displayName = dictionary["Display Name"] as? String else {
return }

guard let searchingFromLatitude = defaults.string(forKey: "SearchingFromLatitude")
else {
print("error 1234324245")
return }

guard let searchingFromLongitude = defaults.string(forKey: "SearchingFromLongitude") else {
print("error 1832835")
return }

guard let DoubleLatitude = Double(searchingFromLatitude) else {
print("error t345345345")
return }
guard let DoubleLongitude = Double(searchingFromLongitude) else {
print("error 55545345")
return }

//////////////Start of distanceFrom code///////////////////
guard let latitude = dictionary["Actual Latitude"] as? String, let longitude = dictionary["Actual Longitude"] as? String, let latDouble = Double(latitude), let longDouble = Double(longitude) else {
return }

let distanceInKms = Database.getDistanceInKms(currentUserLat: DoubleLatitude, currentUserLong: DoubleLongitude, lat: latDouble, long: longDouble)
///////////////End of distance code/////////////////



let user = UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)



completion(user)


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

}
}






ios swift firebase google-cloud-firestore






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 20:41









RX9

6082723




6082723










asked Nov 14 '18 at 11:13









robbieboy888robbieboy888

227




227













  • So whats the issue? your question reads more like a task requirement. are you getting some errors in your code? unexpected results?

    – Scriptable
    Nov 14 '18 at 11:24











  • //append to producers list somehow. have you tried to loop through the users and add each user to the list? producers.append(user) or producers += users

    – Scriptable
    Nov 14 '18 at 11:26











  • Scriptable , yes this is where i am having trouble, how do i access the producers array to append the users from inside the func fetchUsersCurrentUserIsFollowing() ? and then once they are all appended have the screen load?

    – robbieboy888
    Nov 14 '18 at 11:37













  • Value of type 'NewGrapevineSwipeController' has no member 'producers'

    – robbieboy888
    Nov 14 '18 at 11:39











  • as its inside of let cardViewModels: [CardViewModel] =

    – robbieboy888
    Nov 14 '18 at 11:40



















  • So whats the issue? your question reads more like a task requirement. are you getting some errors in your code? unexpected results?

    – Scriptable
    Nov 14 '18 at 11:24











  • //append to producers list somehow. have you tried to loop through the users and add each user to the list? producers.append(user) or producers += users

    – Scriptable
    Nov 14 '18 at 11:26











  • Scriptable , yes this is where i am having trouble, how do i access the producers array to append the users from inside the func fetchUsersCurrentUserIsFollowing() ? and then once they are all appended have the screen load?

    – robbieboy888
    Nov 14 '18 at 11:37













  • Value of type 'NewGrapevineSwipeController' has no member 'producers'

    – robbieboy888
    Nov 14 '18 at 11:39











  • as its inside of let cardViewModels: [CardViewModel] =

    – robbieboy888
    Nov 14 '18 at 11:40

















So whats the issue? your question reads more like a task requirement. are you getting some errors in your code? unexpected results?

– Scriptable
Nov 14 '18 at 11:24





So whats the issue? your question reads more like a task requirement. are you getting some errors in your code? unexpected results?

– Scriptable
Nov 14 '18 at 11:24













//append to producers list somehow. have you tried to loop through the users and add each user to the list? producers.append(user) or producers += users

– Scriptable
Nov 14 '18 at 11:26





//append to producers list somehow. have you tried to loop through the users and add each user to the list? producers.append(user) or producers += users

– Scriptable
Nov 14 '18 at 11:26













Scriptable , yes this is where i am having trouble, how do i access the producers array to append the users from inside the func fetchUsersCurrentUserIsFollowing() ? and then once they are all appended have the screen load?

– robbieboy888
Nov 14 '18 at 11:37







Scriptable , yes this is where i am having trouble, how do i access the producers array to append the users from inside the func fetchUsersCurrentUserIsFollowing() ? and then once they are all appended have the screen load?

– robbieboy888
Nov 14 '18 at 11:37















Value of type 'NewGrapevineSwipeController' has no member 'producers'

– robbieboy888
Nov 14 '18 at 11:39





Value of type 'NewGrapevineSwipeController' has no member 'producers'

– robbieboy888
Nov 14 '18 at 11:39













as its inside of let cardViewModels: [CardViewModel] =

– robbieboy888
Nov 14 '18 at 11:40





as its inside of let cardViewModels: [CardViewModel] =

– robbieboy888
Nov 14 '18 at 11:40












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298913%2fswift-firestore-tinder-fetch-users-and-add-to-an-array-to-load-on-screen-adv%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298913%2fswift-firestore-tinder-fetch-users-and-add-to-an-array-to-load-on-screen-adv%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly