Giter Club home page Giter Club logo

Comments (5)

groue avatar groue commented on September 1, 2024 3

Hello @robert-cronin,

Your sample code looks like you want to drive the content of the database from a textfield. But is it really the case? Don't you want instead to have the content of the database drive the UI?

If this is the case, then the code below, inspired from RxSwift sample code, should work as expected:

// Number of players
let allPlayersRequest = Player.all()
allPlayersRequest.rx
    .fetchCount(in: dbQueue)
    .map { "\($0) players" }
    .bind(to: playerCountLabel.rx.text)

// Maximum score
let maximumScoreRequest = Player
    .select(max(Column("score")))
    .asRequestOf(Int.self)
maximumScoreRequest.rx
    .fetchOne(in: dbQueue)
    .map { "\($0 ?? 0)" }
    .bind(to: maximumScoreLabel.rx.text)

// Name of player 1
Player.filter(key: 1).rx
    .fetchOne(in: dbQueue)
    .map { $0?.name ?? "" }
    .bind(to: playerNameLabel.rx.text)

Now of course, you may sometimes need to update several UI elements on each database change. For example, if you track a single record, you may write something like:

Player.filter(key: 1).rx
    .fetchOne(in: dbQueue)
    .subscribe(onNext: { player in
        if player = player {
            // Update UI for updated Player 1
        } else {
            // Update UI for deleted Player 1
        }
    })

from rxgrdb.

groue avatar groue commented on September 1, 2024 1

Oh, and I have to give you an extra advice when I read:

FLIGHTS.filter(sql: "FLIGHT_ID = '\(currentFlightID)'")

You should really stop embedding raw values in SQL, and start using arguments instead:

FLIGHTS.filter(sql: "FLIGHT_ID = ?", arguments: [currentFlightID})

Another valid option:

FLIGHTS.filter(Column("FLIGHT_ID") == currentFlightID)

Sorry if I sound like a pontificating teacher, but this has to become a reflex: NEVER EMBED RAW VALUES IN YOUR SQL See Avoiding SQL Injection for more information.

from rxgrdb.

robert-cronin avatar robert-cronin commented on September 1, 2024

Thanks groue!
I have tried to implement a simple binding but keep coming up with the following error:
Fatal error: could not convert database value NULL to String

I have a flights table with an aircraft id i want to bind to a textfield, my implementation looks like this:

FLIGHTS.filter(sql: "FLIGHT_ID = '\(currentFlightID)'").rx
    .fetchOne(in: dbQueue)
    .map { $0?.AC_ID ?? "" }
    .bind(to: aircraftIdText.rx.text)

Any ideas on why this is the case given that I know AC_ID is not null?

Thanks!

from rxgrdb.

groue avatar groue commented on September 1, 2024

Any ideas on why this is the case given that I know AC_ID is not null?

Sure. The error message says that someone has tried to decode a NULL column into a non-optional String.

Even if your code only displays AC_ID, it decodes full Flight objects:

struct Flight {
    var FLIGHT_ID: String // requires non-null FLIGHT_ID column
    var AC_ID: String     // requires non-null AC_ID column
    var PILOT_ID: String  // requires non-null PILOT_ID column
    var NAME: String      // requires non-null NAME column
    ...
}

There are only two ways out:

  1. Fix the content of the database: add NOT NULL constraints on columns that must not be null, and find the bug which inserts those invalid nulls.

  2. Make optional properties that should accept null columns:

    struct Flight {
        var FLIGHT_ID: String // mandatory
        var AC_ID: String     // mandatory
        var PILOT_ID: String? // Pilot may not be known yet
        var NAME: String      // mandatory
        ...
    }

from rxgrdb.

robert-cronin avatar robert-cronin commented on September 1, 2024

Yes, I felt a bit nervous writing it like that! It is all functioning as expected now, this library has saved me pages of code! Thanks for writing it 😄

from rxgrdb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.