Fetching Media

Once the SDK integrated and your PlatformClient set-up, you are ready to query your content and retrieve your photos and videos.

Fetching from the server

You can simply retrieve your content stored in Newzulu Platform using the fetchMedias(options:onSuccess:onError:) method of the client.

Here's how to do it:

// Those are the options of your query.
// See bellow how to use them.
let options: PlatformClient.FetchMediasOptions = []

platformClient.fetchMedias(options: options, onSuccess: { result in
    // Your media are available here.
    // See below how to use them.
}, onError: { error in
    // An error occurred.
    // See below how to handle it.
})

Note that all completion blocks are invoked on the main queue. It's safe to update your UI from them.

Using media

The result passed to the onSuccess block is a collection of Media.

The Media object is a wrapper around both images and videos. You can know which type of media it contains with the mediaType property:

switch (media.mediaType) {
    case .image:
        // The media is an image (png, jpeg)
        break
    case .video:
        // The media is a video (mp4, mpeg)
        break
    default:
        // The media is something else (.audio, .text, or .unknown)
        break
}

You can also access:

  • id: A unique ID for this media.
  • mediaURL: URL of the original media at full resolution.
  • thumbnailURL: URL of thumbnail image representing the media. It will always be available for both images and videos. Note that this thumbnail is an image even for videos.
  • title: The title of the media
  • size: Dimensions of the full resolution media.
  • owner: A User object that contains information about the user who uploaded this media.
  • creationDate: When the media was uploaded.

Pagination

The SDK will return no more than 25 elements per query and comes with a pagination API.

The fetchMedias(options:onSuccess:onError:) method takes options as first parameter. These options can define the page to fetch (default is 0). You can create them this way:

let options: PlatformClient.FetchMediasOptions = .page(1)

Let's say you have 40 media in your project. Fetching with .page(0) will return the first 25 and fetching with .page(1) will return the next 15.

You can know if there are more media to query in the onSuccess block: if result.hasMore is true, you can increment the page and start a new query. You can also use result.totalCount to know the total number of media available on the server.

Handling errors

If the SDK fails to retrieve the contents, a NewzuluError is passed to the onError block, which contains information about what went wrong. The error has a reason property that can be one of the following cases:

  • .networkFailure: A network error occurred and the SDK was unable to reach the remote server.
  • .wrongCredentials: The client used its credentials to authenticate prior to fetching the media but they're are not valid. See Authentication.
  • .serverFailure: The remote server encountered an error, you should try again later.
  • .invalidRepresentation: The response from the remote server cannot be understood.
  • .unknown: An unknown internal error occurred.

As NewzuluError is declared in the Newzulu Core framework, you need to import NewzuluCore in your source file when dealing with errors.