My CPU Usgage is constant 100% (Xcode, using SwiftUI) - swift

My SwiftUI App is extremly lagging and I have no Idea why. Even if I only compile this code, my CPU Power goes up to 100%, even if I dont touch anything:
#main
struct Order_Corner_V8App: App {
var body: some Scene {
return WindowGroup {
TabView{
Text("").tabItem {
Text("click")
}
Text("").tabItem {
Text("click")
}
}
}
}
}
I need the TabView. I put my whole Files in an other new Project, the same thing happend again. Maybe its usefull to know that im working with Firebase.
I'm targeting iOS.
I've already watched to my App in 'Instruments', it only says that the problem is my main View.
Please help me.
Here is some image that shows my CPU Data
Thanks, Boothosh

Okay I got the solution: I tried the App on an other phone and everything works fine. I think this is an mistake of my phone, but I don't know what I can do against that.
Thanks, Boothosh

In my case, I found Xcode source control caused the problem
To fix this problem Xcode -> Preferences -> Source Control uncheck Enable Source control. Then restart Xcode, everything will be fine

Related

SwiftUI: DatePicker can't be put in a VStack in Form

See this simple example. Putting DatePicker in VStack has weird layout issue and causes all the content in VStack to not show up (I'm using XCode 13.2 + iOS15.2).
struct ContentView: View {
#State var date: Date = Date()
var body: some View {
Form {
VStack {
DatePicker("Date", selection: $date)
Text("date: \(date)")
}
}
}
}
Replacing the DatePicker with other views (e.g. TextField) works fine. Removing the VStack also works fine. However, I need to have the VStack to make subviews inside it to show up in a single cell.
I googled but wasn't able to find any discussion about this issue. I tried to wrap DatePicker in HStack or ZStack but neither worked. Does anyone know how to work around this issue? Thanks.
A big thank to #Yrb, whose suggestion helps me to find the root cause of the issue.
TL;DR It's a simulator issue. Resetting the simulated device resolved the issue.
More details: I first noticed the issue when I tested my app on the simulator. I thought it was a bug with iOS, especially after I was able to reproduce the issue with the above code in a fresh project. Since #Yrb wasn't able to reproduce the issue, I was about to upgrade my system. However, his suggestion reminded me to try it again on other simulator (I usually use iPhone SE2). It was surprising that other devices didn't have the issue. What's more, while I could reproduce the issue consistently on iPhone SE2 simulator, the preview in XCode just worked fine. So I reset the iPhone SE2 simulator and restarted it (BTW, just restarting it didn't work), the code worked fine!
It's still a mystery what exactly happened in the simulator. But I'm glad to know it's neither my code issue nor an iOS issue.
It took me more than half a day to investigate the issue :( Lessons learned when running into similar weird issue in future: 1) reset simulator 2) try it on physical device also.

SwiftUI Universal Links not working for NFC

Our app uses universal links and the AASA file seems to be working fine because hyperlinks in emails, as well as QR coded URLs both launch the app. They both also cause the SwiftUI .onOpenURL(perform:) function to be called.
Things are a bit more confusing with NFC. The URL is recognised and the app launches, suggesting the issue is not related to AASA. However, the onOpenURL function is not called. Can anyone tell me how to handle universal links from NFC? Ideally I'd like to keep it pure SwiftUI but if I need to use the AppDelegate so be it.
The code in the main SwiftUI file is:
import SwiftUI
#main
struct MyApp: App {
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL(perform: { url in
print("url opened:\(url)")
// prints when opened from URL in email,
// Notes or QR code, but not when opened
// from URL embedded in NFC NDEF
})
}
}
}
I have also tried defining the application(_:continue:restorationHandler:) function in the AppDelegate, but that doesn't get called either.
Thanks for any help.
The solution is to add a continue user activity handler on a suitable view:
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
print("Continue activity \(userActivity)")
guard let url = userActivity.webpageURL else {
return
}
print("User wants to open URL: \(url)")
// TODO same handling as done in onOpenURL()
}
It's not obvious why the same Universal Link received through a click on a link in Safari should be handled differently than the same link read from a tag, but apparently it's not the same thing.
In case it helps anyone with the same issue, I solved the issue by registering a URL schema for the app and then using .onOpenURL(perform:).
Seems a strange way to have to do it but it works as required, so happy days!

context menu, strange constraint warning in console swiftUI

I have create a simple list in swiftUI in order to try the context menu
var body: some View {
List {
ForEach(self.dm.storage) { airportData in
Button(action: {
}) {
ZStack{
Text("Hello list")
}.contextMenu {
Text("Hello")
}
}
}
}
}
but when I run the code on the simulator I get this warning from swiftUI.
any reason why, how can I solve this issue?
thanks for the help
It's a SwiftUI Bug you don't have to worry about. Similar warnings appear if you use a Toogle for example.
This doesn't affect your App and can be ignored (also its annoying to see warnings in the console).
This is fixed as of Xcode beta 12.0. This was confirmed by a response today, to my feedback radar sent on April 1st. I then tested it and it all works perfectly now.
No more strange warnings!

Unable to see preview and unable to see the main window of my app (when running) while using swift ui for macOS developement

So I created a macOS cocoa project called "test" using swift ui in Xcode 11 beta and got problems at the very beginning.
Sorry in advance if my question is dumb.
The first problem is that the automatic preview says it is in pause. If I click resume, I got an error.
So I tried to run the code Xcode generated (I changed nothing in the code.) and it build successfully. When I tried to run the code freshly build, I come across the following error: EXC_BAD_INSTRUCTION
The line highlighted in red is the .frame line in the following piece of code:
import SwiftUI
struct ContentView : View {
var body: some View {
Text("Hello World")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
I'm on macOS Catalina Public Beta 1 so this is not a problem with the version of the system....
So, did I miss something? The tutorials I found online (I mostly looked on Youtube and Apple Dev websites) either show examples for iOS or show examples that are not starting from scratch ( For example on Apple websites, when they are demo-ing codes for macOS apps ).
Thanks in advance
EDIT
I'm adding this picture, it might help:
Actual screen when the app is running with the info of the threads section
Final answer:
Just needed an update Xcode to beta 2 :) .

XPC connection interrupted in Xcode 7 for iOS 9

I recently updated to Xcode 7 and upgraded my iPhone to iOS 9. I have developed and released an iOS app that had worked perfectly fine on the latest version of iOS 8 and Xcode 6.
Upon trying to go through the process of updated the app for iOS 9 support, I am getting the most ridiculously strange error that has left me baffled.
I have done all the syntax corrections automatically through Xcode, and now my app builds properly. It even runs fine at first.
I have a button that segues to a view controller with a WebView. This view controller loads a link that will display either an image, website, or video from youtube. The content is loaded perfectly fine as always. However, the program will crash and reboot the simulator (and my iPhone) and send me to the lock screen when I click the Back button (I am on a navigation stack).
In Xcode, I get the following messages:
XPC Connection Interrupted.
Terminating since there is no system app.
I have Flurry analytics integrated in my app by the way, not sure if thats an issue.
How can I fix this issue? My searches for XPC connections do not seem to return problems similar to mine. I do not even have a clue what an XPC connection is, so why is this in my app anyway?
EDIT: I have found a workaround for the issue. I cannot really say it is a fix.
The crashing was occurring during the use of the method self.navigationController?.popViewControllerAnimated, when set to true. I happened to set this to false, and the crashing stops (now the transition looks awful).
I do not know why this works, and just adds to my confusion.
The problem lied in the storyboard for me as well. I created a new project and laid out the views and everything seemed to be working fine. I found these couple lines in the storyboard source (right click on storyboard and select view as -> source code) which weren't common between the working version and the broken version:
<keyCommands>
<keyCommand/>
</keyCommands>
I have no idea what those lines are supposed to do, or how they crept into my storyboard file, but they were what was crashing the app so hard that the phone had to restart. I removed those lines from my main project and everything worked again.
This error can be caused by executing a loop repeatedly. In my case it was a 'for' loop in which I reset the counting variable. As soon as I added an NSLog in the loop it was obvious.
I just faced the same problem. I don't know if that will help you, but I also think it's coming from the Storyboard:
In my case, the problem is coming from a UITextView. Whenever I try to change the default text inside it, I have this error. If I let the default text or leave it empty, the app works fine. Making an IBOutlet and changing the text programmatically works as well.
I tried with other UI elements, but only the UITextView seems to have this issue.
I have struggled with exact same error. Through a process of elimination I established that it had nothing to do with the any class but had to do with the storyboard. Luckily I keep regular backup copies and I tried to compare storyboards to establish what I had done - but could find nothing obvious.
The backup copy worked fine and I was able to copy my controller classes (from the faulty copy with the changes) into the backup copy and they worked fine.
I think there is a bug possibly in storyboards.
I have same error message when I place a subview in -layoutSubviews method:
-(void)layoutSubviews
{
[super layoutSubviews];
[self populateByImageViews];
}
It causes infinite cycle of layout process and crashes app. Don't place subviews in this place!
Deleting UITextView from the one of the view in Storybord removes the error in my case.
In valueChanged: method of a UIControl, I had the same problem
so I made the code inside valueChanged: to run in main thread and it solved the problem.
#IBAction func valueChanged(sender: AnyObject) {
dispatch_async(dispatch_get_main_queue(), {
//code
}
}
For me was some missing constraints with a UISearchBar, but the error was only in the simulator.
I only add some constraints and works better
For me it was xcode live issues caused by IB_DESIGNABLE
If you have any IB_DESIGNABLE in source files, the system's live tracker will check for issues in StoryBoard too. It may leads to unnecessary building.
To disable it-
Open Storyboard file. Editor -> Automatically Refresh Views (Uncheck)
If you needs to Disable Live issue tracking
XCode -> Preferences -> General -> Issues -> Uncheck Live Issues
Reference
My issue probably originated with some storyboard issue, but I cleaned the project, restarted Xcode AND restarted the simulator app and that fixed it.
When using QLPreviewController, I am confronted with this problem. Error messages as follows,
XPC connection interrupted
_BSMachError: (os/kern) invalid capability (20)
_BSMachError: (os/kern) invalid name (15)
Since XPC means OS X interprocess communication, so I think this can solve the problem, especial when updating the UI
dispatch_async(dispatch_get_main_queue(), ^{
// do what you want to do.
});
For Swift 4+, user
DispatchQueue.main.async {
//Your Code
}