Swift 3 Extension to UIImage to quickly retrieve image from a URL

I spent too long looking for a quick, non-deprecated solution to this simple problem this morning.  Here’s what I came up with. Maybe it will save you a few minutes of searching.  This is valid for Swift 3.

Since I have tableViewCells that need to set images from a URL string, I figured it would be best to make an extension that could be called on the cell configuration:

The extension:

extension UIImageView {
    public func image(fromUrl urlString: String) {
        guard let url = URL(string: urlString) else {
            log.warning("Couldn't create URL from \(urlString)")
            return
        }
        let theTask = URLSession.shared.dataTask(with: url) {
            data, response, error in
            if let response = data {
                DispatchQueue.main.async {
                    self.image = UIImage(data: response)
                }
            }
        }
        theTask.resume()
    }
}

Usage is a one-liner
cell.productImage.image(fromUrl: order.product.imageUrl)

Debugging MongoDB’s 2dsphere geoIndex

When I first learned about MongoDB’s geospatial capabilities similar to PostGIS, I was beyond excited because it meant I would not have to write helper functions to find data along geohash borders.  Currently my data is indexed by geohash, which has great ordering properties, but is a little unwieldy for the “find me x near y”-type queries.

Unfortunately, it seems as though the 2dsphere index doesn’t cover geoWithin queries.  As my collection of geoJSON documents grew from GBs to TBs,  I noticed my tools taking extraordinary amounts of time to perform find queries, which caused me to investigate what was happening under the hood.

First, I double-checked that my geospatial index was present:

mongos> db.my_collection.getIndexes()
[
  {
    "v" : 1,
    "key" : {
      "_id" : 1
    },
    "name" : "_id_",
    "ns" : "telemetry.********"
  },
  {
    "v" : 1,
    "key" : {
      "value.avg_location" : "2dsphere"
    },
    "name" : "geoindex",
    "ns" : "telemetry.*******",
    "2dsphereIndexVersion" : 2
  }
]

So, the index was clearly there.  I also verified that its size was appropriate — it was around 50mb, which seemed fine for this amount of test data.

Next, I ran .explain() on a query to see if it was hitting the index:

Continue reading Debugging MongoDB’s 2dsphere geoIndex

Swift 1.2 and Facebook’s New Login SDK

I’ve had the pleasure of working with Apple’s new Swift over the last few months, and recently ran into some problems integrating Facebook’s API with an iOS 8 app I’m building.

The problem I was having is that, immediately after login, I couldn’t seem to get the user’s access token.

[[code]]czoxMzc6XCI8c3BhbiBjbGFzcz1cInR5cFwiPkZCU0RLQWNjZXNzVG9rZW48L3NwYW4+PHNwYW4gY2xhc3M9XCJwdW5cIj4uPC9zcGFuPjxze1smKiZdfXBhbiBjbGFzcz1cInBsblwiPmN1cnJlbnRBY2Nlc3NUb2tlbjwvc3Bhbj48c3BhbiBjbGFzcz1cInB1blwiPigpPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

was always returning nil, even with the user logged in.

You need to get this token so that you can send it to your Rails app (or whatever you’re using on the back end) and perform Facebook authentication on behalf of your user up there.

Continue reading Swift 1.2 and Facebook’s New Login SDK