-
Hi, I am trying to use ParseSwift with Back4App for my iOS app. I am trying to load some data from Back4App and have it available for use across the entire app, so I thought of using a custom ViewModel. However, simply creating the ViewModel causes the entire app to crash and I get the error When not using the custom ViewModel and only having the data loaded into a single view, I don't run into any errors. Here is my code below. AppDelegate: import UIKit
import ParseSwift
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Task {
do {
try await initializeParse()
} catch {
print("Parse could not be configured")
}
}
return true
}
}
private extension AppDelegate {
func initializeParse() async throws {
try await ParseSwift.initialize(
applicationId: "PARSE_APP_ID",
clientKey: "CLIENT_KEY",
serverURL: URL(string: "https://parseapi.back4app.com")!)
}
} Custom ViewModel and struct: struct relayDictionary: ParseObject {
var originalData: Data?
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var category: String?
var arabicTerm: String?
var transliteration: String?
var litDef: String?
var techDef: String?
}
extension relayDictionary {
init(category: String, arabicTerm: String, transliteration: String, litDef: String, techDef: String) {
self.category = ""
self.arabicTerm = ""
self.transliteration = ""
self.litDef = ""
self.techDef = ""
}
}
class ViewModel: ObservableObject {
@Published var objects = [relayDictionary]()
@Published var error = ParseError?(.init(code: .invalidQuery, message: "Invalid query"))
private var subscriptions = Set<AnyCancellable>()
init() {
fetchDict()
}
func fetchDict() {
let query = relayDictionary.query("category" == "greetings")
let publisher = query
.findPublisher()
.sink(receiveCompletion: { result in
switch result {
case .failure(let error):
//: Publish error.
self.error = error
case .finished:
print("Successfully queried data")
}
},
receiveValue: {
//: Publish found objects
self.objects = $0
print("Found \(self.objects.count), objects: \(self.objects)")
})
publisher.store(in: &subscriptions)
}
} In my ContentView, I have This line seems to cause the error. Removing it causes my app to function again. I am trying to pass dictionaryViewModel as an @EnvironmentObject in other views, but the app doesn't work at all with that line in ContentView. I don't know if initializeParse() is being skipped because the error I get seems to indicate the URL is nil if I'm understanding it correctly. I'm not exactly sure what the cause here is and what the fix here would be. I've been trying to follow the Playground examples but to no avail. Any help would be greatly appreciated! UPDATE: I am now having another issue in my code: The causes another issue when loading data, as it appears to always force an error, so I can't load my data while having if let error = viewModel.error {
Text(error.description)
} in any part of my code. I get the same hardcoded error as I have in the ViewModel. Removing the if statement remedies the issue but of course I need error handling so I'm not sure how to go about this now. Is there a way to ensure that my var error in the ViewModel is declared as ParseError? without the .init()? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Have you tried initializing the ViewModel inside the init() of your view instead of directly initializing it like i.e. init() {
_dictionaryViewModel = StateObject(wrappedValue: ViewModel())
} |
Beta Was this translation helpful? Give feedback.
-
What version of Parse-Swift are you using? I suspect <5.0.1. You shouldn’t see the issue in >5.0.1 as described in more detail here: #37 (reply in thread) The issue is your code is making calls to the SDK before it finishes initialization which can happen if you are using the old AppDelegate life-cycle with SwiftUI. If you are not ready to migrate to a newer version of Parse-Swift, see workarounds: #37 (reply in thread) |
Beta Was this translation helpful? Give feedback.
The Swift Playgrounds is runnable, what happens when you deploy a local server following: https://github.com/netreconlab/Parse-Swift?tab=readme-ov-file#test-drive-parse-swift
and run the actual Playground file which has an optional ParseError:
Parse-Swift/ParseSwift.playground/Pages/18 - SwiftUI - Finding Objects With Custom ViewModel.xcplaygroundpage/Contents.swift
Lines 76 to 108 in b3169f2