Jade Reynolds
Jade Reynolds

Reputation: 301

Swift 3 found nil when Firebase DB persistence is enabled

In my app I am using Firebase db to store my data, I have everything working as it should, with my tableview populating with the data from the db. However I have found that on initial start it is quite slow to download the data, so I enable persistence in AppDelegate. When I did this and ran the app it crashes on load with an unexpectedly found nil error.

FIRDatabase.database().persistenceEnabled = true

is the line I added to AppDelegate under didFinishLaunching section.

The error occurs in this class on the line I have marked

import Foundation
import FirebaseDatabase
import CoreLocation

struct newTracks {

//Declerations
var locationManager = CLLocationManager()
let name: String!
let lat: Double!
let lon: Double!
let countryImage: String!
let link: String!
let ref: FIRDatabaseReference?
let distance: Double


//Initialize
init(name: String, trackId: Int, postcode: String, trackType: String, trackURL: String, locID: Int, lat: Double, lon: Double, phoneNumber: String, email: String, rating: Double, numrating: Double, totalrating: Double, countryImage: String, link: String, distance: Double) {
    self.name = name
    self.ref = nil
    self.lat = lat
    self.lon = lon
    self.countryImage = countryImage
    self.link = link
    self.distance = distance
 }

//Initialize data from Firebase
init(snapshot: FIRDataSnapshot) {

    let snapshotValue = snapshot.value as! [String: AnyObject]
    name = snapshotValue["name"] as! String
    lat = snapshotValue["lat"]as! Double
    lon = snapshotValue["long"]as! Double
    ref = snapshot.ref
    countryImage = snapshotValue["country"] as! String   <--  Unexpectedly found nil
    link = snapshotValue["link"] as! String

    let currentLat = self.locationManager.location!.coordinate.latitude
    let currentLon = self.locationManager.location!.coordinate.longitude
    let myLocation = CLLocation(latitude: currentLat, longitude: currentLon)
    let loc = CLLocation(latitude: lat, longitude: lon)
    let distanceInMiles = round(myLocation.distance(from: loc) / 1609.34)
    distance = distanceInMiles

}

func toAnyObject() -> Any {
    return [
        "name": name,
        "lat": lat,
        "lon": lon,
        "countryImage": countryImage,
        "link": link,
        "distance": distance
    ]

 }

}

I don't get why this works ok when persistence isn't switched on, but when I enable it it crashes. Any help is much appreciated.

Upvotes: 1

Views: 99

Answers (2)

Jade Reynolds
Jade Reynolds

Reputation: 301

I have resolved this by uninstalling the app from my phone, this cleared away the old cached data, seems to load ok now

Upvotes: 0

Vlad Pulichev
Vlad Pulichev

Reputation: 3272

You have marked countryImage as Optional value. Your snapshot may not have value for "country". Try to change:

let countryImage: String!

to:

let countryImage: String

EDIT: Maybe, when you are turning persistance on, your application is taking old data from cache. And this data doesn't have ["country"] in snapshot value. So it founds nil -> throwing error. Check your snapshotValue through breakpoint. And check if it has value for country

Hope it helps

Upvotes: 1

Related Questions