Hi, I am new to swift and IOS development, I was developing an app which can be used to communicating between Apple Watch and iPhone.
Something strange occurred when I was trying to observe the status of the message(UserInfo) sent by func transferUserInfo(_ userInfo: [String : Any] = [:]) -> WCSessionUserInfoTransfer.
I was trying to observe isTransferring(a boolean value) in WCSessionUserInfoTransfer which was returned by the function mentioned above, but it seems cannot be updated even if the message queue was empty, it seems to always be True.
Here is my sample code:
let transfer = session.transferUserInfo(message)
if transfer.isTransferring {
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { timer in
print("Queued message count: \(self.session.outstandingUserInfoTransfers.count), isTransferring:\(transfer.isTransferring)")
if !transfer.isTransferring {
timer.invalidate()
// irrelevant codes...
}
}
} else {
// other irrelevant codes...
}
Appreciate if anyone can help me out of this problem.
Best wishes.
Post
Replies
Boosts
Views
Activity
I have released a watchOS-specific application that retrieves data from the iPhone calendar app and displays it on the Apple Watch.
It uses the calendars function in EKEventStore to retrieve the list of calendars, but it seems that it sometimes fails to retrieve iCloud calendars.
Trouble is, this problem only occurs in a very few users‘ environments, the majority of other users’ environments are able to retrieve them without any problems, and I cannot reproduce it at all in my environment.
Local calendars and Google calendars seem to be retrieved without any problems.
Minimal example code:
import SwiftUI
import EventKit
struct ContentView: View {
let eventStore = EKEventStore()
@State var success: Bool = false
@State var calendarNames: [String] = [String]()
func request() async {
success = (try? await eventStore.requestFullAccessToEvents()) ?? false
}
func list() {
calendarNames = eventStore.calendars(for: .event).map { $0.title }
}
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Access: \(success.description)")
ScrollView {
ForEach(calendarNames, id: \.self) { name in
Text(name)
}
}
}
.onAppear {
Task {
await request()
list()
}
}
.padding()
}
}
I asked the user experiencing the problem to try restarting the iPhone and Apple Watch, reinstalling the app and re-pairing them, but there was no change.
I would appreciate any information you can provide.
Best regards.
I am having trouble accessing and controlling @IBOutlet variables from other ViewControllers.
More specifically, I want to control that @IBOutlet variable based on real-time changes in the variable value of another ViewController.
Control is possible in the viewcontainer that contains the @IBOutlet variable, but when trying to control it from another viewcontainer, the message "Thead1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value" appears.
Is there any possible way to do it?
I think this will be difficult since thread1 in the error message means the main thread, so I tried to create a thread in the viewcontainer that has the @IBOutlet variable and access the variable of another viewcontainer to control the @IBOutlet variable inside the thread, but in this case, the variable value of the other viewcontainer that changes in real time is fixed to the value at the moment the thread is created, and the value that changes in real time after that cannot be retrieved.
And I don't think using threads for this purpose is a good way.
I sincerely ask for help from all the experts.
If I can solve this, it will be easy to create the rest of the app structure.
Sincerely !
While the application is running in the background, I need to take a screenshot and save it in the photos folder. When I try with my test app, it takes only the front of the application. How can I take a screenshot in the background?
Hello all.
This is my code snippet.
RecordListView()
.tabItem {
Label("Record List", systemImage: "list.clipboard")
}
.tag(Tab.RecordList)
When I export localizations, there is no Record List in the .xcloc file.
Then I use LocalizedStringKey for Label and export localizations file, the code is as follows:
let RecordsString:LocalizedStringKey = "Tab.Records"
RecordListView()
.tabItem {
Label(RecordsString, systemImage: "list.clipboard")
}
.tag(Tab.RecordList)
There is still no Tab.Records.
Hi,
I'm working on integrating iCloud Drive functionality into my app. I noticed that the folder for my app appears under /Users/USERNAME/Library/Mobile Documents/MY_CONTAINER, but it doesn't show up in iCloud Drive.
Am I missing a step in the setup or configuration? Any guidance would be appreciated!
Thanks!
Recent Incident with SDKAnalytics Version 11.11.4
We recently released version 11.11.4 of SDKAnalytics for use in the SuperApp. This update introduced several significant changes since the last generated version, 11.11.0. The previous version's primary change was the migration of the integration pipeline and its identifier from OQ6 to FW6. Additionally, we performed refactoring and adjustments to the project's configurations.
In SDKAnalytics, we use XcodeGen to simplify project setup and minimize conflicts. We also rely on CocoaPods to create and manage our Pods/Frameworks.
The SuperApp, where SDKAnalytics is implemented, operates as a centralized ecosystem. Its products and user journeys are segregated into Pods/Frameworks, distributed across multiple repositories, following a multi-repo structure. Currently, the app includes over 300 Pods.
Starting from version 11.11.1 of our SDK, we implemented some important project changes. However, when we released version 11.11.4, which included a new feature for the SuperApp, we encountered a critical issue.
Incident Overview
All SDKs implemented in the SuperApp have their loading times monitored. After updating SDKAnalytics from version 11.11.0 to 11.11.4, the loading time spiked from 200 ms to 3000 ms. Faced with this significant impact, we launched a task force to investigate and resolve the issue.
Investigation and Diagnosis
After several days of investigation, we carefully reviewed the numerous pull requests merged between versions 11.11.0 and 11.11.4. As a strategy, we reverted the project.yml configurations from version 11.11.4 to match those of version 11.11.0. This rollback resolved the loading time issue, indicating that the problem stemmed from the project's configuration.
Upon deeper analysis, we identified the culprit: a single line of configuration. The Bundle ID for the SuperApp follows the pattern com.companyname.enterprise. However, in version 11.11.4, SDKAnalytics adopted a new Bundle ID format: com.companyname.SDKAnalytics. This change directly impacted the SDKAnalytics loading time when implemented in the SuperApp.
Solution
To resolve the issue, we reverted this configuration. We modified the Bundle ID for SDKAnalytics to br.com.SDKAnalytics, removing the com.companyname prefix. After this change, the loading time returned to normal.
Reflection
The lingering question remains: What is the logical explanation for this discrepancy in loading time caused by the Bundle ID configuration? This behavior is unexpected, and we plan to investigate further to understand the underlying technical reasons.
Hi, I'm wondering about one of the properties in the MPNowPlayingInfoCenter: MPNowPlayingInfoPropertyElapsedPlaybackTime. The docs say that updating this property frequently is not required, because the system can automatically calculate elapsed playback time based on the infrequent values we provide.
Is performance harmed by updating this property every second? Should I add some filtering/throttling to update this property infrequently? Am I overthinking this, and it doesn't matter either way?
Kind regards.
I'm attempting to use NWConnection as a websocket given a NWEndpoint returned by NWBrowser, setup like:
let tcpOptions = NWProtocolTCP.Options()
tcpOptions.enableKeepalive = true
tcpOptions.keepaliveIdle = 2
let parameters = NWParameters(tls: nil, tcp: tcpOptions)
parameters.allowLocalEndpointReuse = true
parameters.includePeerToPeer = true
let options = NWProtocolWebSocket.Options()
options.autoReplyPing = true
options.skipHandshake = true
parameters.defaultProtocolStack.applicationProtocols.insert(options, at: 0)
self.connection = NWConnection(to: endpoint, using: parameters)
The initial connection does make it to the ready state but when I first try to send a text message over the websocket, i get
nw_read_request_report [C1] Receive failed with error "Input/output error"
nw_flow_prepare_output_frames Failing the write requests [5: Input/output error]
nw_write_request_report [C1] Send failed with error "Input/output error"
immediately, and the websocket is closed.
Send code here:
let encoder = JSONEncoder()
let dataMessage = try encoder.encode(myMessage)
let messageMetadata = NWProtocolWebSocket.Metadata(opcode: .text)
let context = NWConnection.ContentContext(identifier: "send", metadata: [messageMetadata])
connection.send(content: dataMessage, contentContext: context, completion: .contentProcessed({ error in
if let error = error {
print (error)
}
}))
What would typically cause the Input/output error when writing? Am I doing something obviously wrong or is there something else I can do to get additional debug information?
Thanks in advance for any help.
I am working on the device activity report. and fetched data is loading on the chart. I am developing app using TabbarController. when I go to another tab and come back to the chart screen, it disappears.
Here, I am working on a storyboard using Swift language, and device activity reports can be fetched only with SwiftUI. So, the problem is with it? Following the current code.
@State private var context: DeviceActivityReport.Context = .init(rawValue: "Daily Activity")
@State private var report: DeviceActivityReport?
@State private var filter = DeviceActivityFilter(
segment: .daily(
during: Calendar.current.dateInterval(
of: .day, for: .now
)!
)
// users: .all
// devices: .init([.iPhone, .iPad])
)
@State var isReload: Bool = false
var body: some View {
ZStack {
if isReload {
LoadingView(title: "Data is loading...")
} else if let report = report {
report
} else {
DeviceActivityReport(context, filter: filter)
}
}
.onAppear {
DispatchQueue.main.async {
report = DeviceActivityReport(context, filter: filter)
}
}
}
struct LoadingView: View {
var title: String = "Please wait..."
var body: some View {
HStack {
ProgressView(title)
.font(.system(size: 14, weight: .medium))
.progressViewStyle(.horizontal)
.tint(Color(.darkGray))
.padding(8)
}
.background(Color(.white))
.cornerRadius(8)
.clipped()
}
}
When I try to load the device activity report, it takes too long to load data. Is this because of development time? Will the problem be solved when the app is live on the App Store?
If it does not depend on the development profile, then please give me a solution for the fast data loading in the device activity report extension.
Thank you
Hi!
I'm trying to create a target application that depends on multiple targets that all use the same Swift package that has xcframework binaryTarget. Each component can be successfully assembled individually, but when I try to create a core application that depends on these components, I get an error message on Prepare build stage:
Multiple commands produce '/Users/<USER>/Library/Developer/Xcode/DerivedData/<PROJECT_ID>/Build/Products/Debug/Frameworks/<FW_NAME>.framework/Versions/A'
Target '<COMPONENT1>' has copy command from '/Users/<USER>/Library/Developer/Xcode/DerivedData/<PROJECT_ID>/SourcePackages/artifacts/.../<FW_NAME>/<FW_NAME>.xcframework/macos-arm64_x86_64/<FW_NAME>.framework' to '/Users/<USER>/Library/Developer/Xcode/DerivedData/<PROJECT_ID>/Build/Products/Debug/Frameworks/<FW_NAME>.framework'
Target '<COMPONENT2>' has copy command from '/Users/<USER>/Library/Developer/Xcode/DerivedData/<PROJECT_ID>/SourcePackages/artifacts/.../<FW_NAME>/<FW_NAME>.xcframework/macos-arm64_x86_64/<FW_NAME>.framework' to '/Users/<USER>/Library/Developer/Xcode/DerivedData/<PROJECT_ID>/Build/Products/Debug/Frameworks/<FW_NAME>.framework'
– where <FW_NAME> is a name of the xcframework which is the binaryTarget in the Swift package.
Could someone please explain to me if it is even possible to use xcframeworks packaged in Swift packages in such cases, and if so, how to do it correctly?
Hi, I'm trying to modify the ScreenCaptureKit Sample code by implementing an actor for Metal rendering, but I'm experiencing issues with frame rendering sequence.
My app workflow is:
ScreenCapture -> createFrame -> setRenderData
Metal draw callback -> renderAsync (getData from renderData)
I've added timestamps to verify frame ordering, I also using binarySearch to insert the frame with timestamp, and while the timestamps appear to be in sequence, the actual rendering output seems out of order.
// ScreenCaptureKit sample
func createFrame(for sampleBuffer: CMSampleBuffer) async {
if let surface: IOSurface = getIOSurface(for: sampleBuffer) {
await renderer.setRenderData(surface, timeStamp: sampleBuffer.presentationTimeStamp.seconds)
}
}
class Renderer {
...
func setRenderData(surface: IOSurface, timeStamp: Double) async {
_ = await renderSemaphore.getSetBuffers(
isGet: false,
surface: surface,
timeStamp: timeStamp
)
}
func draw(in view: MTKView) {
Task {
await renderAsync(view)
}
}
func renderAsync(_ view: MTKView) async {
guard await renderSemaphore.beginRender() else { return }
guard let frame = await renderSemaphore.getSetBuffers(
isGet: true, surface: nil, timeStamp: nil
) else {
await renderSemaphore.endRender()
return }
guard let texture = await renderSemaphore.getRenderData(
device: self.device,
surface: frame.surface) else {
await renderSemaphore.endRender()
return
}
guard let commandBuffer = _commandQueue.makeCommandBuffer(),
let renderPassDescriptor = await view.currentRenderPassDescriptor,
let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) else {
await renderSemaphore.endRender()
return
}
// Shaders ..
renderEncoder.endEncoding()
commandBuffer.addCompletedHandler() { @Sendable (_ commandBuffer)-> Swift.Void in
updateFPS()
}
// commit frame in actor
let success = await renderSemaphore.commitFrame(
timeStamp: frame.timeStamp,
commandBuffer: commandBuffer,
drawable: view.currentDrawable!
)
if !success {
print("Frame dropped due to out-of-order timestamp")
}
await renderSemaphore.endRender()
}
}
actor RenderSemaphore {
private var frameBuffers: [FrameData] = []
private var lastReadTimeStamp: Double = 0.0
private var lastCommittedTimeStamp: Double = 0
private var activeTaskCount = 0
private var activeRenderCount = 0
private let maxTasks = 3
private var textureCache: CVMetalTextureCache?
init() {
}
func initTextureCache(device: MTLDevice) {
CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, device, nil, &self.textureCache)
}
func beginRender() -> Bool {
guard activeRenderCount < maxTasks else { return false }
activeRenderCount += 1
return true
}
func endRender() {
if activeRenderCount > 0 {
activeRenderCount -= 1
}
}
func setTextureLoaded(_ loaded: Bool) {
isTextureLoaded = loaded
}
func getSetBuffers(isGet: Bool, surface: IOSurface?, timeStamp: Double?) -> FrameData? {
if isGet {
if !frameBuffers.isEmpty {
let frame = frameBuffers.removeFirst()
if frame.timeStamp > lastReadTimeStamp {
lastReadTimeStamp = frame.timeStamp
print(frame.timeStamp)
return frame
}
}
return nil
} else {
// Set
let frameData = FrameData(
surface: surface!,
timeStamp: timeStamp!
)
// insert to the right position
let insertIndex = binarySearch(for: timeStamp!)
frameBuffers.insert(frameData, at: insertIndex)
return frameData
}
}
private func binarySearch(for timeStamp: Double) -> Int {
var left = 0
var right = frameBuffers.count
while left < right {
let mid = (left + right) / 2
if frameBuffers[mid].timeStamp > timeStamp {
right = mid
} else {
left = mid + 1
}
}
return left
}
// for setRenderDataNormalized
func tryEnterTask() -> Bool {
guard activeTaskCount < maxTasks else { return false }
activeTaskCount += 1
return true
}
func exitTask() {
activeTaskCount -= 1
}
func commitFrame(timeStamp: Double,
commandBuffer: MTLCommandBuffer,
drawable: MTLDrawable) async -> Bool {
guard timeStamp > lastCommittedTimeStamp else {
print("Drop frame at commit: \(timeStamp) <= \(lastCommittedTimeStamp)")
return false
}
commandBuffer.present(drawable)
commandBuffer.commit()
lastCommittedTimeStamp = timeStamp
return true
}
func getRenderData(
device: MTLDevice,
surface: IOSurface,
depthData: [Float]
) -> (MTLTexture, MTLBuffer)? {
let _textureName = "RenderData"
var px: Unmanaged<CVPixelBuffer>?
let status = CVPixelBufferCreateWithIOSurface(kCFAllocatorDefault, surface, nil, &px)
guard status == kCVReturnSuccess, let screenImage = px?.takeRetainedValue() else {
return nil
}
CVMetalTextureCacheFlush(textureCache!, 0)
var texture: CVMetalTexture? = nil
let width = CVPixelBufferGetWidthOfPlane(screenImage, 0)
let height = CVPixelBufferGetHeightOfPlane(screenImage, 0)
let result2 = CVMetalTextureCacheCreateTextureFromImage(
kCFAllocatorDefault,
self.textureCache!,
screenImage,
nil,
MTLPixelFormat.bgra8Unorm,
width,
height,
0, &texture)
guard result2 == kCVReturnSuccess,
let cvTexture = texture,
let mtlTexture = CVMetalTextureGetTexture(cvTexture) else {
return nil
}
mtlTexture.label = _textureName
let depthBuffer = device.makeBuffer(bytes: depthData, length: depthData.count * MemoryLayout<Float>.stride)!
return (mtlTexture, depthBuffer)
}
}
Above's my code - could someone point out what might be wrong?
When swizzling NSURLRequest initialiser and returning a mutable copy, the original instance does not get deallocated and eventually gets leaked and a crash follows after that.
Here's the swizzling setup:
static func swizzleInit() {
let initSel = NSSelectorFromString("initWithURL:cachePolicy:timeoutInterval:")
guard let initMethod = class_getInstanceMethod(NSClassFromString("NSURLRequest"), initSel) else {
return
}
let origInitImp = method_getImplementation(initMethod)
let block: @convention(block) (AnyObject, Any, NSURLRequest.CachePolicy, TimeInterval) -> NSURLRequest = { _self, url, policy, interval in
typealias OrigInit = @convention(c) (AnyObject, Selector, Any, NSURLRequest.CachePolicy, TimeInterval) -> NSURLRequest
let origFunc = unsafeBitCast(origInitImp, to: OrigInit.self)
let request = origFunc(_self, initSel, url, policy, interval)
return request.tagged()
}
let newImplementation = imp_implementationWithBlock(block as Any)
method_setImplementation(initMethod, newImplementation)
}
// create a mutable copy if needed and add a header
private func tagged() -> NSURLRequest {
guard let mutableRequest = self as? NSMutableURLRequest ?? self.mutableCopy() as? NSMutableURLRequest else {
return self
}
mutableRequest.setValue("test", forHTTPHeaderField: "test")
return mutableRequest
}
Then, we have a few test cases:
// memory leak and crash
func testSwizzleNSURLRequestInit() {
let request = NSURLRequest(url: URL(string: "https://example.com")!)
XCTAssertEqual(request.value(forHTTPHeaderField: "test"), "test")
}
// no crash, as the request is mutable, so no copy is created
func testSwizzleNSURLRequestInit2() {
let request = URLRequest(url: URL(string: "https://example.com")!)
XCTAssertEqual(request.value(forHTTPHeaderField: "test"), "test")
}
// no crash, as the request is mutable, so no copy is created
func testSwizzleNSURLRequestInit3() {
let request = NSMutableURLRequest(url: URL(string: "https://example.com")!)
XCTAssertEqual(request.value(forHTTPHeaderField: "test"), "test")
}
// no crash, as the new instance does not get deallocated
// when the test method completes (?)
var request: NSURLRequest?
func testSwizzleNSURLRequestInit4() {
request = NSURLRequest(url: URL(string: "https://example.com")!)
XCTAssertEqual(request?.value(forHTTPHeaderField: "test"), "test")
}
It appears a memory leak occurs only when any other instance except for the original one is being returned from the initialiser.
Is there a workaround to prevent the leak, while allowing for modifications of all requests?
Is there any way to use metal-cpp in a Swift project? I have a platform layer I've written in Swift that handles Window/View creation, as well as event handling, etc. I've been trying to bridge this layer with my C++ layer as you normally would using a pure C interface, but using Metal instances that cross this boundary just doesn't seem to work.
e.g. Currently I initialize a CAMetalLayer for my NSView, setting that as the layer for the view. I've tried passing this Metal layer into my C++ code via a void* pointer through a C interface, and then casting it to a CA::MetalView to be used. When this didn't work, I tried creating the CA::MetalLayer in C++ and passing that back to the Swift layer as a void* pointer, then binding it to a CAMetalLayer type. And of course, this didn't work either.
So are the options for metal-cpp to use either Objective-C or just pure C++ (using AppKit.hpp)? Or am I missing something for how to integrate with Swift?
When i create a intance of swift String :
Let str = String ("Hello")
As swift String are immutable, and when we mutate the value of these like:
str = "Hello world ......." // 200 characters
Swift should internally allocate new memory and copy the content to that buffer for update .
But when i checked the addresses of original and modified str, both are same?
Can you help me understand how this allocation and mutation working internally in swift String?
similiar to
Error when debugging: Cannot creat… | Apple Developer Forums - https://developer.apple.com/forums/thread/651375
Xcode 12 beta 1 po command in de… | Apple Developer Forums - https://developer.apple.com/forums/thread/651157
which do not resolve this issue that I am encountering
Description of problem
I am seeing an error which prevents using lldb debugger on Swift code/projects. It is seen on any Swift or SwiftUI project that I've tried. This is the error displayed in lldb console when first breakpoint is encountered:
Cannot create Swift scratch context (couldn't create a ClangImporter)(lldb)
Xcode Version 12.3 (12C33)
macOS Big Sur Intel M1
Troubleshooting
I originally thought this was also working on an Intel Mac running Big Sur/Xcode 12.3, but was mistaken. Using my customized shell environment on the following setups, I encounter the same couldn't create a ClangImporter.
M1 Mac mini, main account (an "Admin" account)
same M1 Mac mini, new "dev" account (an "Admin" account)
Intel MBP, main account
They are all using an Intel Homebrew install, and my customized shell environment if that provides a clue?
I captured some lldb debugging info by putting expr types in ~/.lldbinit but the outputs were basically identical (when discounting scratch file paaths and memory addresses) compared to the "working clean" account log (described below)
log enable -f /tmp/lldb-log.txt lldb expr types
works in a "clean" user account
I created a new, uncustomized "Standard" testuser account on the M1 Mac mini, and launched the same system Xcode.app. There was no longer this error message, and was able to inspect variables at a swift program breakpoint in Swift context, including po symbol.
Impact
Effectively this makes the debugger in Swift on Xcode projects on my systems essentially unable to inspect Swift contexts' state.
`
init() {
nextOrder = self.AllItems.map{$0.order}.max()
if nextOrder == nil {
nextOrder = 0
}
nextOrder! += 1 // <--- Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
}
`
I have to say, Swift is great - when it works!
I ran into a memory issue that I don't understand why this could happen. For me, It seems like ARC doesn't guarantee thread-safety.
Let see the code below
@propertyWrapper
public struct AtomicCollection<T> {
private var value: [T]
private var lock = NSLock()
public var wrappedValue: [T] {
set {
lock.lock()
defer { lock.unlock() }
value = newValue
}
get {
lock.lock()
defer { lock.unlock() }
return value
}
}
public init(wrappedValue: [T]) {
self.value = wrappedValue
}
}
final class CollectionTest: XCTestCase {
func testExample() throws {
let rounds = 10000
let exp = expectation(description: "test")
exp.expectedFulfillmentCount = rounds
@AtomicCollection var array: [Int] = []
for i in 0..<rounds {
DispatchQueue.global().async {
array.append(i)
exp.fulfill()
}
}
wait(for: [exp])
}
}
It will crash for various reasons (see screenshots below)
I know that the test doesn't reflect typical application usage. My app is quite different from traditional app so the code above is just the simplest form for proof of the issue.
One more thing to mention here is that array.count won't be equal to 10,000 as expected (probably because of copy-on-write snapshot)
So my questions are
Is this a bug/undefined behavior/expected behavior of Swift/Obj-c ARC?
Why this could happen?
Any solutions suggest?
How do you usually deal with thread-safe collection (array, dict, set)?
I make some small program to make dots. Many of them.
I have a Generator which generates dots in a loop:
//reprat until all dots in frame
while !newDots.isEmpty {
virginDots = []
for newDot in newDots {
autoreleasepool{
virginDots.append(
contentsOf: newDot.addDots(in: size, allDots: &result, inSomeWay))
}
newDots = virginDots
}
counter += 1
print ("\(result.count) dots in \(counter) grnerations")
}
Sometimes this loop needs hours/days to finish (depend of inSomeWay settings), so it would be very nice to send partial result to a View, and/or if result is not satisfying — break this loop and start over.
My understanding of Tasks and Concurrency became worse each time I try to understand it, maybe it's my age, maybe language barier. For now, Button with {Task {...}} action doesn't removed Rainbow Wheel from my screen. Killing an app is wrong because killing is wrong.
How to deal with it?