Create App Table programmatically in Swift

Hi Guy`s
I am a old parse user which switched now to backendless. Is there a way to create a App Table programmatically witch Swift2 witch some columns? Hope for help, because this is actually the basic of my app.
PS: Sorry about my english :slight_smile:

Hi Pascal!

This is example for creating data table Contact:
1)

class Contact : NSObject {
    
    var objectId : String?
    var name : String?
    var age: Int?
    var phone : String?
    var title : String?
}
func createContact() {
        
        let contact = Contact()
        contact.name = "Jack Daniels"
        contact.age = 147
        contact.phone = "777-777-777"
        contact.title = "Favorites"
        
        let dataStore = backendless.data.of(Contact.ofClass())
        
        // save object synchronously
        var error: Fault?
        let result = dataStore.save(contact, fault: &error) as? Contact
        if error == nil {
            print("Contact has been saved: \(result!.objectId)")
        }
        else {
            print("Server reported an error: \(error)")
        }
        
        // save object asynchronously
        dataStore.save( contact,
            response: { (result: AnyObject!) -> Void in
                let obj = result as! Contact
                print("Contact has been saved: \(obj.objectId)")
            },
            error: { (fault: Fault!) -> Void in
                print("fServer reported an error: \(fault)")
        })
    }

More examples will be available soon (on documentation page).
Regards,
Kate.

Thank you. But every time I wanna create a table, my app will crash:

Error message:

warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.

Hi Pascal,

You should create your data class (which you are saving in the table) outside of the ViewController class.

Regards,
Slava

I created the project for you - https://drive.google.com/file/d/0B3yyaWhA4ib0b05FWjd4U1dpUW8/view?usp=sharing, it works fine for me, you can try it and compare with your project

Thanks for helping. But I have right now an another problem. If I understand correctly the name of the table is equal the class name.

class Contact : NSObject {

But every user create an another table with their own name. How can I use instant of a fix name a NSString to declare the table name?

Hi Pascal,

Creating a table for every user seems a bit excessive. If the app grows to 1M users, are you going to have 1M tables? It sounds like there may be a better approach with the data model design? Could you shed some light on why you think the current approach is the best?

Regards,
Mark

That is a good argument ^^

But I don’t now an another way. Can you explain that with the data model design?
I wanna create a vocabulary app. Every user creates a own table with three columns (card name, firstLanguage, secondLanguage). An in the two Language columns I will save an Array with the cards (names) in it. I hope you understand what I mean.

How many different card names do you have? Is it a static (not changing) set of values?

The user can create up then unlimited cards. But I don’t thing, that a user creates more then 40 cards. And the cards can be editing

Hi Pascal,

This is helpful. One more question and I should be able to propose a simpler design for you. Earlier you said:

Every user creates a own table with three columns (card name, firstLanguage, secondLanguage)

How is “card name” here different from the name(s) in the first and second language?

For example, say there is a card called “Apple”. In the first language (English), it would be “Apple” and in the second language (Spanish) it would say “Manzana”. Where do all these names go? Or if you organize it differently, please describe with a concrete example.

Cheers,
Mark

Hi Mark,

I will try it to describe it. A user can create a “box” with some cards in it. For example: I do create a “box” called “rooms”. Next I create the cards for the “box”. Every card have to two “sides”. On one side (first language) for example “bedroom”. At the another side (second language) for example “Cuarto”. I will save the firstLanguge cards in firstLanguageArray, secondLanguage in secondLanguageArray. At the end, I will have two arrays and one string (box name) wich I wanna save in the database

Hi Pascal,

Makes sense now. Here’s how I would do it in Backendless:
http://support.backendless.com/public/attachments/5e6586b5e56e9c09d5da0f9ac341ca0d.png</img>

Or in terms of tables defined in Backendless:

Card table:
http://support.backendless.com/public/attachments/22829f864fea85249b588dcb11e93414.png</img>

Box table:
http://support.backendless.com/public/attachments/b87514e77f44d79d651845da6a9dcdef.png</img>

User property to contain the Box object:
http://support.backendless.com/public/attachments/b43740f7427557f942e2b82d4aab9b21.png</img>

Regards,
Mark

5e6586b5e56e9c09d5da0f9ac341ca0d.png

Hi Mark

Tanks for helping. How can I now save new boxes with cards in it and later retrieve it?
Sorry if I annoying you, but I am new and I work hard to understand it. I try really hard to create my first app and I thing with backendless I do have a good “partner” for database stuff :wink:

How can I now save new boxes with cards in it and later retrieve it?

You would use the Data Service API to work with your data. Did you go through the ‘quick start guide’? It shows how to persist objects in Backendless.

Yes I did. I start to understand how it works :wink: But I have still questions:

With following code, I can create a new card.
-But how can I make the relationship between user -> box -> card?
-How can I save an Array in firstLanguage and secondLanguage?

import UIKit




class Card: NSObject {

    var objectId: String?

    var name: String?

    var firstLanguage: String?

    var secondLanguage: String?

}




class ViewController: UIViewController {

    

    var backendless = Backendless.sharedInstance()

    let user: BackendlessUser = BackendlessUser()




    override func viewDidLoad() {

        super.viewDidLoad()




        let createNewCard = Card()

        createNewCard.name = "rooms"

        createNewCard.firstLanguage = "bedroom"

        createNewCard.secondLanguage = "Cuarto"

        backendless.persistenceService.of(Card.ofClass()).save(createNewCard)

    }

}

Sry, I mean: But how can I make the relationship between user -> box -> card programmatically?

To create a relationship between a user and a box, you create a property in BackendlessUser object using the setProperty method. Pass the box object as the value for the property.

To create a relationship between a box and a card, you declare property (or a field) in the Box class with the type of Card. When you save Box, it will store the object and the related object.

You do not need “arrays of languages”. Each card has one first language and one second language (just like you have it in your code).

Mark

Sorry, I don`t really understand what you mean. Can you give me an example please?

Read about relationships here:

https://backendless.com/documentation/data/ios/data_relations.htm
https://backendless.com/documentation/data/ios/data_relations_save_update.htm

You could also read these articles:
https://backendless.com/feature-1-saving-objects-with-relations/
https://backendless.com/feature-95-updating-data-objects-using-api/