Post

Replies

Boosts

Views

Activity

SwiftData - Multiple modelContexts in one modelContainer
In the WWDC presentations, it was mentioned that you could have multiple modelContexts within one modelContainer. Does anyone know of any examples of a project with multiple modelContexts? Specifically, I'm wondering how you distinguish between the two different modelContexts when doing things like @Query. I'd like to keep different sets of data in separate modelContexts and only Query the one that I want to pull from for a particular view. Thanks in advance!
2
2
2.2k
Jul ’23
App Works in Simulator - Black Screen on Launch on Device
I've been building a SwiftUI app, and everything works perfectly fine in the simulator for the exact device I am using (iPhone 13 Pro Max). When I build and launch on my device through Xcode, though, the app only launches to a black screen. When I go to force close the app, the correct view loads for a split second before going away. Has anyone run into this and have any suggestions on what to fix?
2
0
717
Oct ’22
TabView within NavigationStack not behaving properly
I have a TabView within a NavigationStack, and the child views of the TabView contain NavigationLinks and NavigationDestinations. The issue I'm running into is, when tab 1 is the starting view of the TabView, only the NavigationDestinations on tab 1 work. If I start with the TabView displaying tab 2 or 3, the NavigationDestinations on Tabs 1, 2, and 3 all work properly. When starting on tab 1, the error I receive when tapping the NavigationLink on Tab 2 (when it doesn't work) is "A NavigationLink is presenting a value of type “Int” but there is no matching navigation destination visible from the location of the link. The link cannot be activated." I don't think there's anything wrong with the actual NavigationLinks or NavigationDestinations themselves, since it works when launching on tabs 2 or 3, but I cannot for the life of me figure out why starting at tab 1 doesn't work. struct MainView: View {     @State private var selectedView: Int = 1     @Binding var taskList: [ToDoTask]     @Binding var projectList: [Project]     @EnvironmentObject var projectStore: Project.projectList     @Environment(\.scenePhase) private var scenePhase     @State private var navPath = NavigationPath()          var body: some View {         NavigationStack(path: $navPath) {             TabView(selection: $selectedView) {                 NavigationView {                     ContentView(taskList: $taskList, projectList: $projectList)                 }                 .tabItem { Label("Focus", systemImage: "star.fill")                 }                 .tag(1)                 NavigationView {                     ProcessView(taskList: $taskList, projectList: $projectList)                 }                 .tabItem { Label("Refocus", systemImage: "arrow.left.arrow.right") }                 .tag(2)                 NavigationView {                     ReorganizationView(projectList: $projectList, taskList: $taskList)                 }                 .tabItem { Label("Reorganize", systemImage: "exclamationmark.3") }                 .tag(3)             }             .accentColor(Color("ListColor"))             .onChange(of: scenePhase) { phase in                 if phase == .background {                     FileSaveLoad.saveTasks(tasks: taskList) { result in                         if case .failure(let error) = result {                             fatalError(String("\(error)"))                         }                     }                     FileSaveLoad.saveProjects(projects: projectList) { result in                         if case .failure(let error) = result {                           fatalError(String("\(error)"))                         }                     }                 }             }         }     } }
0
3
1k
Oct ’22
Data Persistence and Core Data with SwiftUI
Is there an easy way to add Core Data persistence into an already built SwiftUI app? I've been working through various SwiftUI tutorials to build a very simple reminders/to-do app for myself for the past couple of months. The functionality works, but every time I close the app, I lose the list of tasks. So I started delving into data persistence. What I found is that Core Data seems to be the preferred method; however, it seems like MOST of the tutorials on Core Data are in conjunction with UIKit. Any Core Data tutorials with SwiftUI usually start with the beginning of the project, creating the models in Core Data from the beginning - not trying to implement them on the backend. Is there a way to relate my already created "Task" struct to a Core Data model, such that I don't have to recreate everything? With an extension of Task maybe? Or an easy to way to copy my Task struct over to the Core Data model class/entity? As a beginner, I'm probably approaching this completely wrong, so I'm happy to take a different approach if there's something more preferable. import Foundation import SwiftUI struct Task: Identifiable, Hashable, Codable {     let id: UUID     var taskTitle: String     var taskNotes: String     var isTapped: Bool     init(id:UUID = UUID(), taskTitle:String, taskNotes:String = "", isTapped:Bool = false) {         self.id = id         self.taskTitle = taskTitle         self.taskNotes = taskNotes         self.isTapped = isTapped     } } extension Task {     struct Data {         var taskTitle:String = ""         var taskNotes:String = ""         var isTapped:Bool = false     }     var data: Data {         Data(taskTitle: taskTitle, taskNotes: taskNotes, isTapped: isTapped)     } }
6
1
2.7k
Aug ’22
Updating App Data - iOS Tutorial - Step 7
In the Scrumdinger iOS tutorial, Step 7 of Updating app data asks you to create a new DailyScrum using the newScrumData by passing the following in: let newScrum = DailyScrum(data: newScrumData) However, I receive the following errors because I don't believe the initializer for DailyScrum is set up to accept "data" as an argument: Extra argument 'data' in call Missing arguments for parameters 'title', 'attendees', 'lengthInMinutes', 'theme' in call Can someone help me to understand what I'm doing wrong? Shouldn't there be an initializer in DailyScrum to accept "data" as an argument if I were to want to do it this way? I checked the completed project provided by Apple and the error is still there even in that project, so I'm wondering if there's a step missing in the tutorial
4
0
650
Jun ’22