Tag: Database

Swift3: Uploading Image with Serverless Platform Skygear

This tutorial is written for Skygear, a backend-as-a-service (BaaS) that helps you build serverless apps. Skygear includes features such as push notifications, cloud database, and user authentication.

Go Serverless

Almost any app these days has a photo uploading feature. Whether it’s a social app or a review platform, a photo uploading feature is essential for gathering user-generated content. A feature like this shouldn’t take a whole day to write. Here’s a walkthrough for you to develop a simple app that allows you to upload images and share them using Skygear.

As mentioned, Skygear is a serverless platform for building apps. One of the features, Skygear CloudDB, enables you to upload your user-generated content  to the cloud storage. This content can be  images, videos, or even audio files. The files will be hosted in the cloud and made available for your mobile app clients. You can download the Swift photo feed demo and run on Xcode to see how photo upload works.

Setup your Skygear backend

First thing, in this example, you have to initialize Skygear before any Skygear app reference is created or used. If you have already done this for another Skygear feature, you can skip this step.

Initialize Skygear:

  1. Create your app backend on Skygear Portal (2 mins)
  2. Install the Skygear SDK (10 mins)
  3. Import SKYKit module & other configuration into your code (3 mins)

You can follow  this step-by-step iOS guide to initialize Skygear.

Uploading a photo

Ok. Let’s start implementing the uploading photos feature. In Skygear, you can make use of Asset to store file references, such as images and videos on the database. Let’s say, your user has taken a photo. In order to have the photo uploaded, we will call the uploadAsset API to upload the photo image file as an asset.

In the above sample code, we define an uploadable SKYAsset and set the chosen image data as the asset data. We also set the mimeType of the asset as image/jpg. Upon calling uploadAssest, the asset assigned will be uploaded. Then we pass to the completionHandler to handle success / fail upload case.

You can check the full list of supported mime types here

Saving the asset with a photo record

Since we want to query the photo later, we have to save the image to the cloud database.
In Skygear, an asset can only be saved with a record but not as a standalone upload. We will create a photo record and assign the uploaded asset as asset of the record.

Fetching the uploaded photos

Now, we want to load back the photos we’ve uploaded through the app.

To access the photos, we need to query the photo records. We can directly access the uploaded asset with SKYAsset.url .

As a result, we have imageAsset.url in hand , so we can load back the image and display it on any device.

To learn more about Skygear
In addition to images, you can also upload other file types (such as voice messages, PDF files, live recordings, etc) to the serverless platform. Please see detailed documentation about file uploading here: Skygear iOS Assets (File Upload). If you need my support on trying it out, just ping me (David) on Skygear Google Group.

Tell us what you think and subscribe to our blog for more project updates!

Follow us on Twitter to get updates.

SQLAlchemy in batches: Generating a top playlist

SQLAlchemy is arguably the most powerful and ubiquitous ORM framework for Python.

At Oursky, we have been using SQLAlchemy for quite a period of time and appreciated the flexibility and elegance it provides over the Data Mapper abstraction. No doubt, it works very well for modern web applications but what about long-running background jobs? Would the abstraction get in your ways? (tl;dr: yes, but we still prefer it)

Here are some hands-on experiences from us.

We built a popular iOS application with a song recommendation system at the backend. The system suggests a top list for 20 popular songs.

Previously our editors hand-picked popular songs by download count and gather a new playlist as a recommendation to users. Now, we want to automate this process and generate the playlist weekly.

Continue reading

Hands on Realm Database for Android projects

In one of the Android app projects at Oursky, we started using Realm for data caching. So users won’t have to look at blank pages when they log back to the app, waiting for remote API results.

We think it’s a minimalistic yet reliable solution, and is worth giving it a shot for your next app. Just beware of the differences between versions, follow the conventions, and pay extra attention when using with other libraries.

More is discussed below in details and with example snippets.

What is Realm?

Realm Database

Realm Database

Realm is the mobile database solution that  proposed as  a replacement for SQLite & Core Data.

It’s like using SQLite with an ORM (as those who used SQLAlchemy may have experience), with a lot of awesome and convenient methods for data transactions.

However, Realm is NOT built on top of SQLite. It persists on its own persistence engine.

Realm is  also cross-platform that supports both iOS and Android, so developers who write Java, Swift, Objective-C can share the same Realm files painlessly.

Continue reading

© 2017 Oursky Code Blog

Theme by Anders NorenUp ↑