In an attempt to implement MVVM into my project, I have moved out a Firebase
code structure to a repository. I then want to return an array from that Firebase
loop back to the class the Firebase
code previously resided (MyCollectionViewClass).
Declared above viewDidLoad:
var imageArray = [Recipe]()
In viewDidLoad:
imageArray = firebaseReference!.getFirebaseData(dataRecieverStringCollectionView:
(self.dataRecieverStringCollectionView as? String)!)
firebaseReference!.getFirebaseData
is a reference to the repository class and its function.
self.dataRecieverStringCollectionView
is a string value passed into the function (I have checked so that it indeed contains the proper string value)
I have tried a number of different wrapping/unwrapping combinations without success. The error I'm getting is Unexpectedly found nil while unwrapping an Optional value
Everything worked fine before, so it's just this code transition that has messed up the project.
The actual Firebase
code in the repository class looks like this:
func getFirebaseData(dataRecieverStringCollectionView: String) -> Array<Recipe>{
databaseRef = Database.database().reference()
storageRef = Storage.storage().reference()
databaseRef!.child(dataRecieverStringCollectionView).observeSingleEvent(of: .value, with: {(snapshot) in
for item in snapshot.children.allObjects as! [DataSnapshot] {
let thisItem = item.value as! NSDictionary
let tempRecipe = Recipe()
tempRecipe.fbKey = item.key
tempRecipe.recipeImageObject = (thisItem["recipeImageFirebase"] as! String)
tempRecipe.recipeHeaderObject = (thisItem["recipeHeaderFirebase"] as! String)
tempRecipe.recipeTextObject = (thisItem["recipeIngredientsTextFirebase"] as! String)
self.imageArray.append(tempRecipe)
}
})
return imageArray
}
Not really sure what the issue is. I return a [Recipe]
and try to store it in a [Recipe]
variable in the class calling the method. Is the return type wrong in the function? Something else I've missed? Thanks in advance! :)