Post

Replies

Boosts

Views

Activity

Getting data from SQLite in SwiftUI
I have an app that I wrote using the Apache Cordova Libraries that saves a small amount of data in a local SQLite database. I'm currently rewriting the app using SwiftUI and Core Data. Is there any way of getting the data from the existing SQLite database so I can process it and re-save in Core Data? I've googled and it seems that SwiftUI doesn't play well with SQLite. It looks as though it may be possible but the options I've seen don't seem very satisfactory and are overly complicated. Are there any suggestions for a good method of recovering the data? I don't want the users to lose their information. Thanks
1
0
1.4k
Oct ’21
Changing a fetch request
I'm doing a FetchRequest before the body of my page but can't add in the predicate as the id that I'm after isn't available until after self is available. I've tried moving the query to a method triggered by a button press, but I get a segmentation fault. Is there any way that I can add the predicate after the initial query to get my collection? Alternatively, is there a way of passing in the id to the page direct? (Its a detail page, but its not triggered by selecting a list item) If I hard code the id, everything works as expected, I just can't seem to get the id in the original query. thanks
7
0
2.0k
Jun ’21
Check on FetchRequest not working
Hi, I have the following page (this is a simplified version) where there is a FetchRequest to get a record from CoreData based on an id. This id is obtained from some json that is stored in UserDefaults (returned by the getLodgeData method). import SwiftUI struct TestView: View {     var lodge: Lodge = getLodgeData()     @State var lodgeid: Int = -1     var body: some View {         TestViewPage(lodgeid: lodgeid)             .onAppear(perform: {                 self.lodgeid = Int(lodge.LodgeID) ?? 0         })     } } struct TestViewPage: View {     var lodgeid: Int     @Environment(\.managedObjectContext) var managedObjectContext     var lodge: Lodge = getLodgeData()     @State var favourite = false     //this gets the list     @FetchRequest var cdlodges: FetchedResults<CDLodge>     init(lodgeid: Int) {         self.lodgeid = lodgeid         self._cdlodges = FetchRequest(             entity: CDLodge.entity(),             sortDescriptors: [                 NSSortDescriptor(keyPath: \CDLodge.lodge, ascending: true),             ],             predicate: NSPredicate(format: "lodgeid == \(lodgeid)")         )         if cdlodges.isEmpty == false {             //not empty so set fav to true             favourite = true         }         else {             //not a favourite so set to false             favourite = false         }     }     var body: some View {         NavigationView {             ScrollView {                 VStack(alignment: .leading) {                       VStack {                           switch favourite {                           case false:                               Button(action: {                                   ///save the lodge in core data                                   //...                                   favourite = setFav()                               })                               {                                   Image("favicon_off")                               }                           case true:                               Button(action: {                                   ///delete the lodge from Core Data                                   //...                               })                               {                                   Image("favicon_on")                               }                           }                       }                       Button(action: {                           if cdlodges.isEmpty == false {                               //not empty so set fav to true                               favourite = true                           }                           else {                               //not a favourite so set to false                               favourite = false                           }                       }) {                           Text("testing")                       }                 }                 .padding()             }///end of scrollview             .navigationBarTitle("")             .navigationBarHidden(true)         } //end of navigationview     } } I want to set a variable (favourite) based on if the dataset that the FetchRequest returns is empty or not. I tried putting the check after the request, but it always comes back as empty. This is odd as I know that a row is being returned. I added a button (marked test) that runs the same check, and that works fine so I can only sumise that the initial check is being done before the resultset is finished being populated. Is there a way that I can do the check when the query updates? At this point I feel that I'm missing something simple, but I just can't seem to see it. Any help would be greatly appreciated. thanks
1
0
1.7k
Jun ’21
Getting data from SQLite in SwiftUI
I have an app that I wrote using the Apache Cordova Libraries that saves a small amount of data in a local SQLite database. I'm currently rewriting the app using SwiftUI and Core Data. Is there any way of getting the data from the existing SQLite database so I can process it and re-save in Core Data? I've googled and it seems that SwiftUI doesn't play well with SQLite. It looks as though it may be possible but the options I've seen don't seem very satisfactory and are overly complicated. Are there any suggestions for a good method of recovering the data? I don't want the users to lose their information. Thanks
Replies
1
Boosts
0
Views
1.4k
Activity
Oct ’21
Changing a fetch request
I'm doing a FetchRequest before the body of my page but can't add in the predicate as the id that I'm after isn't available until after self is available. I've tried moving the query to a method triggered by a button press, but I get a segmentation fault. Is there any way that I can add the predicate after the initial query to get my collection? Alternatively, is there a way of passing in the id to the page direct? (Its a detail page, but its not triggered by selecting a list item) If I hard code the id, everything works as expected, I just can't seem to get the id in the original query. thanks
Replies
7
Boosts
0
Views
2.0k
Activity
Jun ’21
Check on FetchRequest not working
Hi, I have the following page (this is a simplified version) where there is a FetchRequest to get a record from CoreData based on an id. This id is obtained from some json that is stored in UserDefaults (returned by the getLodgeData method). import SwiftUI struct TestView: View {     var lodge: Lodge = getLodgeData()     @State var lodgeid: Int = -1     var body: some View {         TestViewPage(lodgeid: lodgeid)             .onAppear(perform: {                 self.lodgeid = Int(lodge.LodgeID) ?? 0         })     } } struct TestViewPage: View {     var lodgeid: Int     @Environment(\.managedObjectContext) var managedObjectContext     var lodge: Lodge = getLodgeData()     @State var favourite = false     //this gets the list     @FetchRequest var cdlodges: FetchedResults<CDLodge>     init(lodgeid: Int) {         self.lodgeid = lodgeid         self._cdlodges = FetchRequest(             entity: CDLodge.entity(),             sortDescriptors: [                 NSSortDescriptor(keyPath: \CDLodge.lodge, ascending: true),             ],             predicate: NSPredicate(format: "lodgeid == \(lodgeid)")         )         if cdlodges.isEmpty == false {             //not empty so set fav to true             favourite = true         }         else {             //not a favourite so set to false             favourite = false         }     }     var body: some View {         NavigationView {             ScrollView {                 VStack(alignment: .leading) {                       VStack {                           switch favourite {                           case false:                               Button(action: {                                   ///save the lodge in core data                                   //...                                   favourite = setFav()                               })                               {                                   Image("favicon_off")                               }                           case true:                               Button(action: {                                   ///delete the lodge from Core Data                                   //...                               })                               {                                   Image("favicon_on")                               }                           }                       }                       Button(action: {                           if cdlodges.isEmpty == false {                               //not empty so set fav to true                               favourite = true                           }                           else {                               //not a favourite so set to false                               favourite = false                           }                       }) {                           Text("testing")                       }                 }                 .padding()             }///end of scrollview             .navigationBarTitle("")             .navigationBarHidden(true)         } //end of navigationview     } } I want to set a variable (favourite) based on if the dataset that the FetchRequest returns is empty or not. I tried putting the check after the request, but it always comes back as empty. This is odd as I know that a row is being returned. I added a button (marked test) that runs the same check, and that works fine so I can only sumise that the initial check is being done before the resultset is finished being populated. Is there a way that I can do the check when the query updates? At this point I feel that I'm missing something simple, but I just can't seem to see it. Any help would be greatly appreciated. thanks
Replies
1
Boosts
0
Views
1.7k
Activity
Jun ’21