1

I'm working on an iOS app that works with Apache Usergrid. So far everything is working, I can register users, log in, query...

btw: I'm working with Usergrid 2.1.0 release, running on my own server.

Now I want to store the users profile picture. I do it like so:

let image = UIImage(named:"user.png")!     
let asset = UsergridAsset(fileName: "profilepic", image: image, imageContentType: .Png)!

Usergrid.currentUser!.uploadAsset(asset, progress: nil) { (response, asset, error) -> Void in
    if response.ok {
        print("Picture saved")
    } else {
        self.showErrorMessage("Picture couldn't be saved")
        print(response.description)
    }
}

this seems to be working, since when looking into Portal I can see something about "file-metadata" in my users' entity. Now the problem: How do I get the Image back? I tried the following:

Usergrid.currentUser?.downloadAsset("image/png", progress: nil, completion: { (asset, error) -> Void in
    if (error == nil) {
        if let image = UIImage(data: (asset?.data)!) {
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.profileImageButton.setImage(image, forState: .Normal)
            })         
        }
    }
}

but everytime I get the Error "Entity does not have an asset attached" when in fact I can see the image with the help of Firefox RESTClient. What am I doing wrong?

  • Could there be something wrong in the Swift SDK? When I build my own UsergridRequest, manually setting the path and header for the file I want to retrieve, I do get a reply with image data, which of course throw an error since this cannot be converted to JSON. Trying to find the error... – ChrisNewman Feb 24 '16 at 12:28

1 Answers1

2

The error "Entity does not have an asset attached" occurs when entity.hasAsset is false aka when entity.asset == nil && entity.fileMetaData?.contentLength <= 0.

If you are uploading the asset and directly downloading it right after, the Usergrid.currentUser might have not updated its fileMetaData or asset instance property.

I would try updating the current user by calling Usergrid.currentUser!.reload() before trying to retrieve the assets data.

Usergrid.currentUser!.reload() { response in
    if response.ok {
         Usergrid.currentUser!.downloadAsset("image/png", progress:nil) { downloadedAsset, error in
             // Handle downloaded asset here.
         }
     }
}

It is also worth noting that a more up to date (beta) version of the swift sdk can be found on my fork here.

robertwalsh
  • 171
  • 1
  • 5
  • Also I added a test case located [here](https://github.com/RobertWalsh/usergrid/blob/master/sdks/swift/Tests/ASSET_Tests.swift#L150) that could help you when implementing this. – robertwalsh Feb 24 '16 at 22:21
  • Thanks for your reply! I'm using your beta version of the sdk now, and it is really weird. I wrote a little test app that actually works in the simulator but on the device it says no 'asset attached', while in my main app it doesn't work neither on the simulator nor on the device. Exception: it works right after saving an image asset to the user, doing your suggested reload right after saving. I normally call currentUser!.downloadAsset() inside viewDidAppear() since i want my imageView to be loaded when applying the image. could this be a problem? – ChrisNewman Feb 26 '16 at 13:45
  • This sounds really strange. Really hard to say without being able too see the code. Is there any chance you could upload the test app to github and I could take a look at it? – robertwalsh Feb 29 '16 at 21:15