Reputation: 3830
Without getting too complicated, how to upload a single quantum of data from an iOS app (using Swift) to an Azure mobile service database?
Specifically, how to actually use the table insert method which requires a MSItemBlock closure?
Here's the decorated method I'm hunting down:
itemTable.insert(<#item: NSDictionary?#>, completion: <#MSItemBlock?#>)
Upvotes: 1
Views: 4557
Reputation: 1717
The iOS Swift quickstart for Azure Mobile Services will be available for download next portal update. In the meantime you can see it here, here's the insert line:
Inserting an item to the table should look like:
let originalItem = ["key":"value"];
itemTable.insert(originalItem) {
(item, error) in
// Logic here
}
Upvotes: 1
Reputation: 3830
Sometimes you just want to do one simple thing without building an entirely perfect solution.
In my case, all I wanted was to insert/upload a record from my iOS app (using core data) to my Azure mobile service database. I didn’t want to use table controllers or full table management objects (not yet anyway) - just a brief “proof-of-concept” to test out a basic save and upload pattern.
What I had in place:
What I wanted to do:
What I did:
Logged into my Azure portal and navigated to my mobile service
Selected the iOS platform, then opened the “Connect an existing iOS app” link.
Followed the on screen instructions to:
Download the iOS SDK
Create an "Item" table (by simply clicking the provided button)
What I did next:
Dragged the downloaded sdk into my Xcode project
In my Bridging-Header.h file (generated for me by Xcode when I had previously run “Create NSManagedObject Subclass…” commands on my core data models) I added this import:
#import "WindowsAzureMobileServices/WindowsAzureMobileServices.h"
In AppDelegate.swift I added a constant reference to the AzureClient:
let client = MSClient(applicationURLString: "https://mymobileapp.azure-mobile.net/", applicationKey: “aAaBbBcCc…")
Finally:
Here's the function I was wanting to use:
itemTable.insert(<#item: NSDictionary?#>, completion: <#MSItemBlock?#>)
Accordingly, and in swift parlance, I used the following to insert a record up to Azure heaven:
var client = AppDelegate().client // To reference my constant in AppDelegate.swift
var itemTable:MSTable = client.tableWithName("Item")
var itemToInsert:NSDictionary = ["text":"My Awesome Item 1"]
itemTable.insert(itemToInsert,
completion: {
insertedItem, error in
if error{
println("error: \(error)")
}
else{
println("Success!")
}
}
)
The expected/returned MSItemBlock in the insert method stumped me for a while until I read up a little on swift closures vs objc blocks.
Disclaimer: The above snippet worked for me in this particular case but in truth I'm a drunken monkey playing with matches at the moment, so I do realise there must be far more efficient ways of doing this. However, I could find absolutely nothing on the web about the MSItemBlock and its pattern of usage in Swift, so hopefully this might prove a starting point for anybody else struggling with a similar issue.
Upvotes: 3