Hello,
I have made a picker that is filled with data from an API endpoint. The data is retrieved and shown properly, however, when trying to change the selection on the picker, it does not work.
For simple demonstration i have put the picker in a empty view. Here is the code:
import SwiftUI
struct TestView: View {
struct Subject: Hashable {
let subjectId: Int
let subjectName: String
}
@State var subjects: [Subject] = []
@State var selectedSubject: Subject? = nil
var body: some View {
VStack {
Picker(selection: $selectedSubject, label: Text("Kies een vak")) {
ForEach(subjects, id: \.self) { subject in
Text(subject.subjectName).tag(subject)
}
}
.frame(width: 300, height: 50)
.background(Color.black.opacity(0.05))
.cornerRadius(10)
.onChange(of: selectedSubject) { newSubject in
print("Chosen subject: \(newSubject?.subjectId ?? -1)")
}
Text("Chosen subject: \(selectedSubject?.subjectId ?? -1)")
}
.onAppear {
Api().extractSubjects { subjects in
DispatchQueue.main.async {
self.subjects = subjects.map { TestView.Subject(subjectId: $0.subjectId, subjectName: $0.subjectName) }
}
}
}
}
}
To better illustrate what I mean i have made a screenrecording:
I hope you guys and girls can help me out. If you need more info, please let me know!
Any help or suggestion is greatly appreciated, thanks!
Explore the various UI frameworks available for building app interfaces. Discuss the use cases for different frameworks, share best practices, and get help with specific framework-related questions.
Post
Replies
Boosts
Views
Activity
Is it possible to disable NavigationLink transition animation in SwiftUI? To be more precisely. I want to disable it only for a few links/views. Not the whole app
I'm building a macOS app using SwiftUI and I recently updated to xcode 14.3. Since then I've been debugging why none of my animations were working, it turned out that the NavigationSplitView or NavigationStack are somehow interfering with all the animations from withAnimation to .transition() and everything in between.
Is anyone else experiencing this, knows a work around or knows why this is happening?
After implementation of deep linking and when we click link in notes or email then it should call
.onContinueUserActivity in Swiftui.
But it's not happening.
Can someone help me, how to do it.
Hi,
It is known that if a SwiftUI view contains an @ObservedObject or @StateObject property, the View will inherit @MainActor isolation from the property wrappers.
Similarly, the body view-builder is also marked @MainActor.
What I'm wondering is why the whole SwiftUI View protocol isn't marked @MainActor. It seems to be a deliberate decision, but AFAICT it would make a lot of sense for all data and operations defined in a view to have main-actor isolation unless marked nonisolated.
I'm currently adding @MainActor annotations to an existing codebase, and it's a bit awkward that some views automatically gain this attribute one way or another, while others need it explicitly applied. Is there a rationale that can be shared, or is this something which may be revised in future versions of the framework?
Can someone please shed some light? I have an app that uses Core Data and CloudKit, up until the last version, I was able to sync data between devices but now after I added two new Entities for some reason it stopped syncing between devices.
Here is how I did the change:
Created a new Core Data container.
Added the new Entities and their Attributes
Tested the new Entities locally to be able to send the new schema to CloudKit.
Went to CloudKit and made sure that the new Entities and Attributes were reflected on the Developent database.
Deploy Schema Cahnges.
Went to the Production database to make sure the new schema was deployed; and it was, both databases look the same.
Testing:
Tested in the simulator and with a real device and everything syncs, even the new Entities.
If I download the app from the App Store on two different devices they do NOT sync.
Based on the procedure I'm describing above, is there any important step I may have missed when doing the migration?
I'm not sure if this is related to the syncing issue but after testing a few times, I no longer can turn the iCloud on, I get the following message when I try to turn iCloud Sync On.
CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate resetAfterError:andKeepContainer:]: <NSCloudKitMirroringDelegate: 0x282c488c0> - resetting internal state after error: Error Domain=NSCocoaErrorDomain Code=134410 "CloudKit setup failed because there is another instance of this persistent store actively syncing with CloudKit in this process." UserInfo={NSURL=file:///var/mobile/Containers/Data/Application/73F19BC7-4538-4098-85C7-484B36192CF3/Library/Application%20Support/CoreDataContainer.sqlite, NSLocalizedFailureReason=CloudKit setup failed because there is another instance of this persistent store actively syncing with CloudKit in this process., NSUnderlyingException=Illegal attempt to register a second handler for activity identifier com.apple.coredata.cloudkit.activity.setup.8D4C04F6-8040-445A-9447-E5646484521}
Any idea of what could be wrong and preventing the devices from syncing? Any idea or suggestion is welcome.
Thanks
I have a view which shows some data based on a complex calculation. Let's say I need to parse some input string and transform it in some way.
private func someComplexCalculation(_ input: String) -> String {
// ...
}
The most naive approach would be to perform this in the view's body:
struct MyView: View {
let input: String
var body: some View {
Text(someComplexCalculation(input))
}
}
But of course, we want to keep body nice and fast because SwiftUI may call it very often. The text to be displayed will be constant for a particular view, in a particular place in the hierarchy (barring some major events such as locale changes, which can basically change the entire UI anyway).
So the next idea would be to hoist the calculation out of body in to the view's initialiser:
struct MyView: View {
let value: String
init(_ value: String) {
self.value = someComplexCalculation(value)
}
var body: some View {
Text(value)
}
}
Except that this view's initialiser will be called in the body of its parent, so this isn't really much of a win at all.
So the next idea is that we need to associate the cached data with the underlying view itself somehow. From what we are told about SwiftUI, that's what @State does.
struct MyView: View {
@State var value: String
init(_ value: String) {
self._value = State(initialValue: someComplexCalculation(value))
}
var body: some View {
Text(value)
}
}
Except... apparently this is not recommended because SwiftUI won't honour the value set in the initialiser.
That's kind of okay for my purposes - the contents won't change, and every view with the same identity (place in the hierarchy) will be provided the same input. The real problem emerges when we look at the documentation for @State. Its initialiser takes a value directly, so we're still going to perform this expensive calculation every time; we'll just discard the value immediately afterwards and take one which the framework memoised.
Which brings me on to my final approach - @StateObject. Unlike @State, its initialiser takes an autoclosure, so we won't recompute the value every time. But it should still be stored in a way that is bound to the underlying view, thereby giving me a place to stash memoised values.
struct MyView: View {
final class Cache {
var transformed: String
init(input: String) {
self.transformed = someComplexCalculation(value)
}
}
@StateObject var cache: Cache
init(_ value: String) {
self._cache = StateObject(wrappedValue: Cache(input: value))
}
var body: some View {
Text(cache.transformed)
}
}
I haven't been able to find much in the way of others online using @StateObject for this purpose, so I'd like to ask - is there some other solution I'm overlooking? Is this considered a misuse of @StateObject for some reason? The documentation for the StateObject initialiser says:
Initialize using external data
If the initial state of a state object depends on external data, you can call this initializer directly. However, use caution when doing this, because SwiftUI only initializes the object once during the lifetime of the view — even if you call the state object initializer more than once — which might result in unexpected behavior.
Which seems fine. This seems like exactly what I want.
I have a performance issue with a Mac SwiftUI app. Using instruments I see hangs reported. When I zero in on a hang I see that the time profiler reports most of the hang -- in one example 658 out of 687 ms -- being in 'static AppName.$main() [inlined]'.
I think that is saying my app is busy, but busy doing what?
The "hangs" are related to SwiftUI table processing. User experience is something like this: select an item, see the view changes based upon selection show up a second or two later. The duration of the hang increases with the number of items in the table. I'm testing with 100 ~ 1200 table entries.
Hi everyone
I am fighting with a weird problem
in my app I use swiftui and ai do not use tabs, but a hierarchy of view created by me that switch one to the other with a slide animation when I tap one of the elements
i don't use gesture, but only ontap.
The same app launched on two different iPhone has two different behaviours
on the iPhone 11 no problem, the animation is fluid and responsive, on the iPhone 10 I got some delay, and in the console I can read Gesture: System gesture gate timed out
I have searched but I have not found anything useful and more than this it is extremely difficult to find some info about this warning/error
How can I solve this?
on the iPhone 10 it seems like the animation got stuck for 1 seconds prior to be executed in the correct way
Our app crashes intermittently when calling performSegue(withIdentifier:,sender:). The segue does exist, as most of the time it works fine. The destination view controller only has its view as a connection. Crash report excerpt below. Uploading the crash report isn't working for some reason, so I'll try to do so as a reply.
The stack trace shows searchForClientURL... as the last part of our code that gets executed.
I'm curious if I should look at things in our code that happen while the segue is being performed such as prepare(for:, sender:), the init of the destination controller, etc. or not because they're not showing up in the stack trace.
Thanks for any help!
0 CoreFoundation 0x19e310e38 __exceptionPreprocess + 164 (NSException.m:202)
1 libobjc.A.dylib 0x19749f8d8 objc_exception_throw + 60 (objc-exception.mm:356)
2 UIKitCore 0x1a1306630 __66-[UIStoryboardPushSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 880 (UIStoryboardPushSegueTemplate.m:58)
3 UIKitCore 0x1a08f3c84 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 172 (UIStoryboardSegueTemplate.m:134)
4 UIKitCore 0x1a08f3ba4 -[UIStoryboardSegueTemplate _perform:] + 68 (UIStoryboardSegueTemplate.m:121)
5 UIKitCore 0x1a0cc74c0 -[UIViewController performSegueWithIdentifier:sender:] + 300 (UIViewController.m:5017)
6 AWApp 0x10061ce38 closure #1 in ClientSelectorViewController.searchForClientURL(nameOrGUID:shouldTestSession:) + 3616 (ClientSelectorViewController.swift:231)
I've been working on an iOS project for the iPhone and would like to support running it on macOS computers with Apple Silicon. In the Targets / Supported Destinations we added "Mac (Designed for iPhone)" but experienced Thread 1: EXC_BAD_ACCESS crashes immediately when we tried to run it.
We've isolated it down to Stepper UI elements in our view. Starting a new project and just trying to present a single Stepper in the ContentView, we get the same crash.
Here is code that presents the issue:
// ContentView.swift
import SwiftUI
struct ContentView: View {
@State var someValue = 5
var body: some View {
VStack {
Stepper("Stepper", value: $someValue, in: 0...10)
}
}
}
When run from Xcode on an iOS device or the simulator, it runs fine.
Trying to run it on the Mac, it crashes here:
// Stepper_01App.swift
import SwiftUI
@main // <-- Thread 1: EXC_BAD_ACCESS (code=2, address=0x16a643f70)
struct Stepper_01App: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Xcode 14.3 (14E222b), MacOS Ventura 13.3.1 (a), Mac mini M2.
Target: Mac (Designed for iPhone)
We have verified that the same code crashes on all the Apple Silicon Macs we have access to. Searching the Internet and Apple Developer forums I dont find other reports, so I kind of feel there must be some level of either user error or system/project misconfiguration going on?
If any iOS app that used Steppers was just crashing when trying to run on a Mac, it seems like this would be a big deal.
If anyone has input or can point out what we need to do differently, it would be appreciated!
Animation is glitching when I tap on search bar
Below is the code
struct CustomTabBar: View {
@State var searchText = ""
var data = [1,2,3,4,5,6,7,8,9,10]
var body: some View {
NavigationStack {
ScrollView(.vertical) {
ForEach(data, id: \.self) { d in
box
}
}
.navigationTitle("Search")
.searchable(text: $searchText)
}
}
var box: some View {
Rectangle()
.frame(width: 75, height: 50)
.cornerRadius(10)
}
}
I am learning SwiftUI, I want to observe an AVPlayer status so I know when the videos is paused or not.
My current approach is more less like this:
I have VideosView that holds a list of a videos (in ZStack cards).
VideoViews has a VideosViewModel.
in VideosView i am calling in onAppear VideosViewModel.getItems...
struct ItemModel: Identifiable, Codable, Hashable, Equatable {
var id: String
var author: String // video owner
var url: URL? // url to the video
var player: AVPlayer? // AVPlayer created based on self.url...
mutating func setPlayer(_ avPlayer: AVPlayer) {
self.player = avPlayer
}
}
// vm
class FeedViewModel: ObservableObject {
@Published private(set) var items: [ItemModel] = []
func getItems() async {
do {
// fetch data from the API
let data = try await dataService.fetchFeeds()
// download and attach videos
downloadFeedVideos(data)
} catch {
// ....
}
}
private func downloadFeedVideos(_ feeds: [ItemModel]) {
for index in feeds.indices {
var item = feeds[index]
if let videoURL = item.url {
self.downloadQueue.queueDownloadIfFileNotExists(
videoURL,
DownloadOperation(
session: URLSession.shared,
downloadTaskURL: videoURL,
completionHandler: { [weak self] (localURL, response, error) in
guard let tempUrl = localURL else { return }
let saveResult = self?.fileManagerService.saveInTemp(tempUrl, fileName: videoURL.lastPathComponent)
switch saveResult {
case .success(let savedURL):
DispatchQueue.main.async {
// maybe this is a wrong place to have it?
item.setPlayer(AVPlayer(url: savedURL))
self?.items.append(item)
if self?.items.count ?? 0 > 1 {
// once first video is downloaded, use all device cores to fetch next videos
// all newest iOS devices has 6 cores
self?.downloadQueue.setMaxConcurrentOperationCount(.max)
}
}
case .none: break
case .failure(_):
EventTracking().track("Video download fail", [
"id": item.id,
"ulr": videoURL.absoluteString.decodeURL()
])
}
}), { fileCacheURL in
// file already downloaded
DispatchQueue.main.async {
item.setPlayer(AVPlayer(url: fileCacheURL))
self.items.append(item)
}
})
}
}
}
}
I found this article with some pseudo-code of how to track video playback state but I'm not sure how to implement it in my code....
https://developer.apple.com/documentation/avfoundation/media_playback/observing_playback_state
In Monterey, when a user was at the top of a ScrollView implemented inside of a NSHostingController's view (that was itself embedded in a window with a NSToolbar), the window's toolbar background would be hidden until the user scrolled from the top.
In Ventura, this behavior is different, with the toolbar's background visible all of the time unless a traditional NSScrollView is used (which means no SwiftUI).
Is there the ability to change this behavior within SwiftUI some how now?
Hello and thanks for reading my post.
I have a SwiftUI view, the users should be able to click a button and take printout of that view. Clicking on the button should open the standard print sheet (select printer, pages, layout, etc.).
How can I implement such a functionality? I have been trying hard without any success. Please help.
It is an iPad app, using Xcode 14.3
I'm in the process of migrating to the Observation framework but it seems like it is not compatible with didSet. I cannot find information about if this is just not supported or a new approach needs to be implemented?
import Observation
@Observable class MySettings {
var windowSize: CGSize = .zero
var isInFullscreen = false
var scalingMode: ScalingMode = .scaled {
didSet {
...
}
}
...
}
This code triggers this error:
Instance member 'scalingMode' cannot be used on type 'MySettings'; did you mean to use a value of this type instead?
Anyone knows what needs to be done? Thanks!
I am using a LayzVStack embedded into a ScrollView. The list items are fetched from a core data store by using a @FetchResult or I tested it also with the new @Query command coming with SwiftData.
The list has one hundred items 1, 2, 3, ..., 100.
The user scrolled the ScrollView so that items 50, 51, ... 60 are visible on screen.
Now new data will be fetched from the server and updates the CoreData or SwiftData model. When I add new items to the end of the list (e.g 101, 102, 103, ...) then the ScrollView is keeping its position.
Opposite to this when I add new items to the top (0, -1, -2, -3, ...) then the ScrollView scrolls down.
Is there a way with the new SwiftData and SwiftUI ScrollView modifiers to update my list model without scrolling like with UIKit where you can query and set the scroll offset pixel wise?
Is .refreshable supposed to do anything on macOS? Works fine on iOS and iPadOS but it's not triggered on macOS. It's available since macOS 12 but the documentation doesn't mention anything about that.
https://developer.apple.com/documentation/swiftui/view/refreshable(action:)
The code for @State doesn't seem to work.
struct DonutListView: View {
var donutList: DonutList
@State private var donutToAdd: Donut?
var body: some View {
List(donutList.donuts) { DonutView(donut: $0) }
Button("Add Donut") { donutToAdd = Donut() }
.sheet(item: $donutToAdd) { // <-- would need a "donut in"
TextField("Name", text: $donutToAdd.name) // <-- donutToAdd is optional and I'm not sure how it would be unwrapped
Button("Save") {
donutList.donuts.append(donutToAdd)
donutToAdd = nil
}
Button("Cancel") { donutToAdd = nil }
}
}
}
Does anyone have a fix for this?
Thanks,
Dan!
Simplely, when we set UITextView.inputView and then call becomeFirstResponder, but the custom inputView could not show expectedly just like before. We test this code in iOS17 and below, while only iOS17 does not work.
And xcode console print these logs:
Failed to retrieve snapshot.
-[RTIInputSystemClient remoteTextInputSessionWithID:performInputOperation:] perform input operation requires a valid sessionID
-[RTIInputSystemClient remoteTextInputSessionWithID:performInputOperation:] perform input operation requires a valid sessionID
-[RTIInputSystemClient remoteTextInputSessionWithID:performInputOperation:] perform input operation requires a valid sessionID
-[RTIInputSystemClient remoteTextInputSessionWithID:performInputOperation:] perform input operation requires a valid sessionID
Unsupported action selector setShiftStatesNeededInDestination:autoShifted:shiftLocked:
Unsupported action selector setShiftStatesNeededInDestination:autoShifted:shiftLocked:
Unsupported action selector setShiftStatesNeededInDestination:autoShifted:shiftLocked:
Unsupported action selector setShiftStatesNeededInDestination:autoShifted:shiftLocked: