mike4aday / SwiftlySalesforce

The Swift-est way to build native mobile apps that connect to Salesforce.
MIT License
136 stars 43 forks source link

Login migration path for Swift Apps #129

Closed perbrondum closed 3 years ago

perbrondum commented 3 years ago

Can you please outline a migration path for existing Apps using salesforce.query()?

Looks like SwiftlySalesforce no longer needs the global salesforce variable. Can we still use the following:

struct DB { static let connectedApp: ConnectedApp = { let consumerKey = "CONS KEY" let callbackURL = URL(string: "CALL BACK")! return ConnectedApp(consumerKey: consumerKey, callbackURL: callbackURL) }()

static let salesforce: Salesforce = {
    return Salesforce(connectedApp: DB.connectedApp)
}()

}

and call is using: DB.salesforce.query()?

mike4aday commented 3 years ago

@perbrondum - version 9 is designed to be more SwiftUI-friendly. It does away with the need for a global salesforce value, as you noted. And it doesn't have a Salesforce type, so the static variable you defined wouldn't compile.

If you create a file called Salesforce.json in your bundle and put the consumer key and callback URL there, you could create ConnectedApp wherever you need it, e.g. ConnectedApp().query(soql: "SELECT Id FROM Account"). (The no-arg constructor can throw if it doesn't find Salesforce.json in the bundle.)

Here's another example of creating ConnectedApp at the call site. This way you don't need a global salesforce variable, and you don't have to pass a reference between many SwiftUI views or in the SwiftUI environment.

mike4aday commented 3 years ago

@perbrondum I'll close this issue - please re-open if I didn't address your question.

perbrondum commented 3 years ago

I actually tried using ConnectedApp():

ConnectedApp().query(soql: “Select ID from account”)

But in my case it fails with 'Generic parameter 'T' could not be inferred’

Per B Jakobsen CEO Tap2Sales.com @.***

On Jun 7, 2021 at 2:04:59 AM, Michael Epstein @.***> wrote:

@perbrondum https://github.com/perbrondum - version 9 is designed to be more SwiftUI-friendly. It does away with the need for a global salesforce value, as you noted. And it doesn't have a Salesforce type, so the static variable you defined wouldn't compile.

If you create a file called Salesforce.json in your bundle and put the consumer key and callback URL there, you could create ConnectedApp wherever you need it, e.g. ConnectedApp().query(soql: "SELECT Id FROM Account"). (The no-arg constructor can throw if it doesn't find Salesforce.json in the bundle.)

Here's another example https://github.com/mike4aday/MySalesforceAccounts/blob/2fa839ad30155d384712c3b155dddb2ed19119b8/MySalesforceAccounts/MyAccountsLoader.swift#L23 of creating ConnectedApp at the call site. This way you don't need a global salesforce variable, and you don't have to pass a reference between many SwiftUI views or in the SwiftUI environment.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mike4aday/SwiftlySalesforce/issues/129#issuecomment-855486167, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALCYWDDZHSENHMLNNATVUMLTRQEKXANCNFSM46GLANHA .

perbrondum commented 3 years ago

Or in the original context:

    *var* subscriptions = Set<AnyCancellable>()

    ConnectedApp().query(soql: "SELECT Id FROM Account", allowsLogin:

true).sink(receiveCompletion: { (completion) in

        *switch* completion {

        *case* *let* .failure(error):

            print("Failed to execute 2 queries in insert content, Error

: (error)")

        *case* .finished:

            print("Completed 2 queries in insert content")

        }

    }, receiveValue: { orgData *in*

//

    }).store(in: &subscriptions)

Still returns : ‘Generic parameter ’T’ could not be inferred’

Obviously I’m doing something wrong here but I’m trying to access impact on our existing App.

Per B Jakobsen CEO Tap2Sales.com @.***

On Jun 7, 2021 at 2:08:21 PM, Per Jakobsen @.***> wrote:

I actually tried using ConnectedApp():

ConnectedApp().query(soql: “Select ID from account”)

But in my case it fails with 'Generic parameter 'T' could not be inferred’

Per B Jakobsen CEO Tap2Sales.com @.***

On Jun 7, 2021 at 2:04:59 AM, Michael Epstein @.***> wrote:

@perbrondum https://github.com/perbrondum - version 9 is designed to be more SwiftUI-friendly. It does away with the need for a global salesforce value, as you noted. And it doesn't have a Salesforce type, so the static variable you defined wouldn't compile.

If you create a file called Salesforce.json in your bundle and put the consumer key and callback URL there, you could create ConnectedApp wherever you need it, e.g. ConnectedApp().query(soql: "SELECT Id FROM Account"). (The no-arg constructor can throw if it doesn't find Salesforce.json in the bundle.)

Here's another example https://github.com/mike4aday/MySalesforceAccounts/blob/2fa839ad30155d384712c3b155dddb2ed19119b8/MySalesforceAccounts/MyAccountsLoader.swift#L23 of creating ConnectedApp at the call site. This way you don't need a global salesforce variable, and you don't have to pass a reference between many SwiftUI views or in the SwiftUI environment.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mike4aday/SwiftlySalesforce/issues/129#issuecomment-855486167, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALCYWDDZHSENHMLNNATVUMLTRQEKXANCNFSM46GLANHA .

mike4aday commented 3 years ago

@perbrondum the compiler just needs a hint about the type of value received, for example:

receiveValue: { (orgData: QueryResult<SalesforceRecord>) in

(And any errors thrown by ConnectedApp( ) have to be handled)

perbrondum commented 3 years ago

Thank you.

    *var* subscriptions = Set<AnyCancellable>()

    *do* {

        *try*? ConnectedApp().query(soql: "SELECT Id FROM Account",

allowsLogin: true).sink(receiveCompletion: { (completion) in

            *switch* completion {

            *case* *let* .failure(error):

                print("Failed to execute query in insert content, Error

: (error)")

            *case* .finished:

                print("Completed query")

            }

        },   receiveValue: { (orgData: QueryResult<SalesforceRecord>)

in

        }).store(in: &subscriptions)

    }

Per B Jakobsen CEO Tap2Sales.com @.***

On Jun 7, 2021 at 5:29:23 PM, Michael Epstein @.***> wrote:

@perbrondum https://github.com/perbrondum the compiler just needs a hint about the type of value received, for example:

receiveValue: { (orgData: QueryResult) in

(And any errors thrown by ConnectedApp( ) have to be handled)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mike4aday/SwiftlySalesforce/issues/129#issuecomment-856036662, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALCYWDGJBLAKM6QGXTUTBATTRTQVHANCNFSM46GLANHA .