Post

Replies

Boosts

Views

Activity

Can I add a CKDatabaseOperation to the queue belonging to a CKContainer object using CKContainer.add(_:)?
Can I add a CKDatabaseOperation to the queue belonging to a CKContainer object using CKContainer.add(_:)? The CKContainer.add(_:) method takes a CKOperation argument, so it will take a subclass of CKOperation as far as I can tell. I can't find any documentation on this. What I'm trying to figure out is which queue should I add a CKFetchRecordsOperation object to? Should I use CKDatabase.add(:), or CKContainer.add(:)? let fetchRecordsOperation = CKFetchRecordsOperation.fetchCurrentUserRecordOperation() I need the CKRecord for the current user for the entire CKContainer, or for the privateCloudDatabase and the sharedCloudDatabase to be specific. If I pass the CKFetchRecordsOperation object to the queue for CKDatabase, would that not return the current CKRecord for the current user for that CKDatabase only? I suspect that if there is a separate CKRecord representing the current user in each database in a container, that they would each hold identical values, even identical recordName fields. Is that the case?
1
0
584
Aug ’22
How do I avoid nested closures or clauses?
I need to take some suggestions from other programmers about something. When we have to use an asynchronous closure, often because the Framework provides only an asynchronous method for something we can't do without, it seems to prevent us from using the guard statement, which we need to make the code easier to read. The guard statement keeps us from making nested if statements, which makes the code difficult to read if there are many lines of code within a set curly braces. I would see a closing curly brace, and not be able to see the what the if condition is for that if clause (what's between the set of curly braces). Putting comments at the closing curly brace works, but that means I would still have to find the if condition at the beginning of the if clause. I could collapse the clause where the curly braces closes a clause, but then I have to find that collapsed clause, because Xcode scrolls the content of the editor upward, so that the beginning of the clause is hidden upwards toward the top, and I don't know how far up it is. This problem that the guard statement solves is also a problem with completion handlers, which we have to have with any asynchronous code. One approach to this problem is to avoid nested curly braces. How do I do that when I have to have asynchronous clause of code? If there is someway I can return a variable of type Result from a function the classical way instead of in a completion handler, I could avoid nested closures or clauses. One way I've found is to use the DispatchSemaphore or any statement to instructs code execution to wait for an asynchronous closure end and then return. There's something I don't like about this that I'm not sure what it is. Anytime there is a code instruction to wait, it makes me uncertain to its effectiveness. One thing that would help is if Xcode would show in the editor the beginning of collapsed code, so that when I click on the left side of the code editor to collapse a closure/clause when I'm seeing the bottom of the closure, Xcode would scroll downward so that the code editor shows me the code at the beginning of the clause, so that I wouldn't have to scroll upward to find the code that introduces the clause of code. I could file a bug report with Apple. Anyone have any contributions to any solutions to this problem? By the way, what is the Apple or Swift word for I'm calling a clause? What, for example, is the code that is enclosed between curly braces? Am I using the words clause and closure each time I used them?
3
0
1.6k
Aug ’22
How to cache Contacts in iOS?
I want to check if my code to cache contacts data while my iOS app is running is done well or not. Anyone have any samples I can look at. I did a search on the internet and cannot find much. Maybe code to cache any time of cache would help for me to look at. I am using Xcode 13.4.1, and my app is intended to install on iOS 12.0 up to the most current iOS version. I seem to remember a search class in one of Apple's frameworks for iOS. I don't remember if that was part of the Contacts Framework. I also will have a cache for the Contacts data that I store in CloudKit, so I take that into account to be looking for any way that might make the Contacts cache and the CloudKit cache work together.
0
0
674
Aug ’22
what assert statement does and what it does in CloudKitShare sample project?
What does the following line do in the code snippet from CloudKitShare Apple sample Xcode project that I show after that?   assert(zoneID == self.zone.zoneID) // Update the server change token. // operation.recordZoneChangeTokensUpdatedBlock = { (zoneID, serverChangeToken, _) in   assert(zoneID == self.zone.zoneID)   self.setServerChangeToken(newToken: serverChangeToken) } The documentation for the assert statement says that when the project build is -0none and also in Playground, the code terminates completely, and not just exit from the current code, do I understand that correctly? The documentation seems to assume I understand C-style programming.
1
0
976
Sep ’22
How do I write thread-safe code that uses a completionHandler with a function that delegates code to an instance of OperationQueue?
I've been using the CloudKitShare sample code found here as a sample to help me write code for my app. I want to use performWriterBlock and performReaderBlockAndWait as found in BaseLocalCache using a completionHandler without violating the purposes of the design of the code, which focuses on being thread-safe. I include code from CloudKitShare below that are pertinent to my question. I include the comments that explain the code. I wrote comments to identify which code is mine. I would like to be able to use an escaping completionHandler if possible. Does using an escaping completionHandler still comply with principles of thread-safe code, or does it in any way violate the purpose of the design of this sample code to be thread-safe? If I use an escaping completionHandler, I would need to consider when the completionHandler actually runs relative to other code outside of the scope of the actual perform function that uses the BaseLocalCache perform block. I would for one thing need to be aware of what other code runs in my project between the time the method executes and the time operationQueue in BaseLocalCache actually executes the block of code and thus the completionHandler. class BaseLocalCache { // A CloudKit task can be a single operation (CKDatabaseOperation) // or multiple operations that you chain together. // Provide an operation queue to get more flexibility on CloudKit operation management. // lazy var operationQueue: OperationQueue = OperationQueue() // This sample ... // // This sample uses this dispatch queue to implement the following logics: // - It serializes Writer blocks. // - The reader block can be concurrent, but it needs to wait for the enqueued writer blocks to complete. // // To achieve that, this sample uses the following pattern: // - Use a concurrent queue, cacheQueue. // - Use cacheQueue.async(flags: .barrier) {} to execute writer blocks. // - Use cacheQueue.sync(){} to execute reader blocks. The queue is concurrent, // so reader blocks can be concurrent, unless any writer blocks are in the way. // Note that Writer blocks block the reader, so they need to be as small as possible. // private lazy var cacheQueue: DispatchQueue = { return DispatchQueue(label: "LocalCache", attributes: .concurrent) }() func performWriterBlock(_ writerBlock: @escaping () -> Void) { cacheQueue.async(flags: .barrier) { writerBlock() } } func performReaderBlockAndWait<T>(_ readerBlock: () -> T) -> T { return cacheQueue.sync { return readerBlock() } } } final class TopicLocalCache: BaseLocalCache { private var serverChangeToken: CKServerChangeToken? func setServerChangeToken(newToken: CKServerChangeToken?) { performWriterBlock { self.serverChangeToken = newToken } } func getServerChangeToken() -> CKServerChangeToken? { return performReaderBlockAndWait { return self.serverChangeToken } } // Trial: How to use escaping completionHandler? with a performWriterBlock func setServerChangeToken(newToken: CKServerChangeToken?, completionHandler: @escaping (Result<Void, Error>)->Void) { performWriterBlock { self.serverChangeToken = newToken completionHandler(.success(Void())) } } // Trial: How to use escaping completionHandler? with a performReaderBlockAndWait func getServerChangeToken(completionHandler: (Result<CKServerChangeToken, Error>)->Void) { performReaderBlockAndWait { if let serverChangeToken = self.serverChangeToken { completionHandler(.success(serverChangeToken)) } else { completionHandler(.failure(NSError(domain: "nil CKServerChangeToken", code: 0))) } } } }
0
0
747
Sep ’22
What does DispatchQueue.AutoreleaseFrequency.workItem mean?
The documentation for DispatchQueue.AutoreleaseFrequency.workItem says , "The queue configures an autorelease pool before the execution of a block, and releases the objects in that pool after the block finishes executing." Does this mean that the dispatch queue releases each work item or each code in ()->Void after each one finishes executing? When it says, "releases the objects in that pool er the block finishes executing", it makes it sound like there is only one block executing and then the entire pool is released. I'm making sure I verify that I understand this correctly.
1
0
1.1k
Sep ’22
Where is the message instance property of MFMessageComposeViewController?
Why am I not seeing the message instance property of MFMessageComposeViewController? When I type the following, I get a code-time error message in Xcode that says "Value of type 'MFMessageComposeViewController' has no member 'message'" messageComposeViewController.message // error message: "Value of type 'MFMessageComposeViewController' has no member 'message'" There is nothing that explains this in the official Apple documentation: Documentation/Message UI/MFMessageComposeViewController/message
0
0
1.5k
Sep ’22
How do I create a perpetual "wizard" in iOS that allows the user to see the same options over and over until the user selects the option to end the wizard?
What is the most effective approach to to creating a type of "wizard" interface that takes the user through a process step-by-step, and allows the user to take the steps in any order as he goes along? For instance, the user could start out by selecting from three options: select contacts, select addresses, or selecting a message to send. When he selects one of those options, say select contacts, he sees the interface to allow him to perform that selection, and he sees the other two options to select at any time, which would be select addresses and select message. Say he then selects the option to select a message, he then sees an interface to selecta message and he also sees the other two options. And this continues indefinitely until he selects to send the message. I think the usual view controllers and segues would work for this. Would I need only one subclass of UINavigationController and three subclasses of UIViewControllers? Could this lead to any problems? Is it possible to do this with view controllers and segues? Is there a better way? I do use Swift, but this question doesn't require that I use only Swift, as you can see.
1
0
1k
Sep ’22
Does a parent record in CloudKit have to be in the same CKDatabase as a child record?
I have used code to test whether a child record and a parent record have to be in the same zone. They do. Now... Does a parent record have to be in the same CKDatabase as a child record?
Replies
1
Boosts
0
Views
1.2k
Activity
Aug ’22
Can I add a CKDatabaseOperation to the queue belonging to a CKContainer object using CKContainer.add(_:)?
Can I add a CKDatabaseOperation to the queue belonging to a CKContainer object using CKContainer.add(_:)? The CKContainer.add(_:) method takes a CKOperation argument, so it will take a subclass of CKOperation as far as I can tell. I can't find any documentation on this. What I'm trying to figure out is which queue should I add a CKFetchRecordsOperation object to? Should I use CKDatabase.add(:), or CKContainer.add(:)? let fetchRecordsOperation = CKFetchRecordsOperation.fetchCurrentUserRecordOperation() I need the CKRecord for the current user for the entire CKContainer, or for the privateCloudDatabase and the sharedCloudDatabase to be specific. If I pass the CKFetchRecordsOperation object to the queue for CKDatabase, would that not return the current CKRecord for the current user for that CKDatabase only? I suspect that if there is a separate CKRecord representing the current user in each database in a container, that they would each hold identical values, even identical recordName fields. Is that the case?
Replies
1
Boosts
0
Views
584
Activity
Aug ’22
Which way to get currently logged in user in iOS?
Under what conditions should we use CKContainer.fetchUserRecordID() and then fetch the CKRecord object, and under what conditions should we use CKFetchRecordsOperation.fetchCurrentUserRecordOperation().
Replies
1
Boosts
0
Views
465
Activity
Aug ’22
How do I avoid nested closures or clauses?
I need to take some suggestions from other programmers about something. When we have to use an asynchronous closure, often because the Framework provides only an asynchronous method for something we can't do without, it seems to prevent us from using the guard statement, which we need to make the code easier to read. The guard statement keeps us from making nested if statements, which makes the code difficult to read if there are many lines of code within a set curly braces. I would see a closing curly brace, and not be able to see the what the if condition is for that if clause (what's between the set of curly braces). Putting comments at the closing curly brace works, but that means I would still have to find the if condition at the beginning of the if clause. I could collapse the clause where the curly braces closes a clause, but then I have to find that collapsed clause, because Xcode scrolls the content of the editor upward, so that the beginning of the clause is hidden upwards toward the top, and I don't know how far up it is. This problem that the guard statement solves is also a problem with completion handlers, which we have to have with any asynchronous code. One approach to this problem is to avoid nested curly braces. How do I do that when I have to have asynchronous clause of code? If there is someway I can return a variable of type Result from a function the classical way instead of in a completion handler, I could avoid nested closures or clauses. One way I've found is to use the DispatchSemaphore or any statement to instructs code execution to wait for an asynchronous closure end and then return. There's something I don't like about this that I'm not sure what it is. Anytime there is a code instruction to wait, it makes me uncertain to its effectiveness. One thing that would help is if Xcode would show in the editor the beginning of collapsed code, so that when I click on the left side of the code editor to collapse a closure/clause when I'm seeing the bottom of the closure, Xcode would scroll downward so that the code editor shows me the code at the beginning of the clause, so that I wouldn't have to scroll upward to find the code that introduces the clause of code. I could file a bug report with Apple. Anyone have any contributions to any solutions to this problem? By the way, what is the Apple or Swift word for I'm calling a clause? What, for example, is the code that is enclosed between curly braces? Am I using the words clause and closure each time I used them?
Replies
3
Boosts
0
Views
1.6k
Activity
Aug ’22
How to cache Contacts in iOS?
I want to check if my code to cache contacts data while my iOS app is running is done well or not. Anyone have any samples I can look at. I did a search on the internet and cannot find much. Maybe code to cache any time of cache would help for me to look at. I am using Xcode 13.4.1, and my app is intended to install on iOS 12.0 up to the most current iOS version. I seem to remember a search class in one of Apple's frameworks for iOS. I don't remember if that was part of the Contacts Framework. I also will have a cache for the Contacts data that I store in CloudKit, so I take that into account to be looking for any way that might make the Contacts cache and the CloudKit cache work together.
Replies
0
Boosts
0
Views
674
Activity
Aug ’22
How well does NSCache and NSPurgeableData work for caching Contacts data or anything else?
Anyone have any words of wisdom about using NSCache and NSPurgeableData for caching in an iOS app, specifically to cache Contacts data from the device contact store? Under what conditions does this work well?
Replies
0
Boosts
0
Views
300
Activity
Aug ’22
what assert statement does and what it does in CloudKitShare sample project?
What does the following line do in the code snippet from CloudKitShare Apple sample Xcode project that I show after that?   assert(zoneID == self.zone.zoneID) // Update the server change token. // operation.recordZoneChangeTokensUpdatedBlock = { (zoneID, serverChangeToken, _) in   assert(zoneID == self.zone.zoneID)   self.setServerChangeToken(newToken: serverChangeToken) } The documentation for the assert statement says that when the project build is -0none and also in Playground, the code terminates completely, and not just exit from the current code, do I understand that correctly? The documentation seems to assume I understand C-style programming.
Replies
1
Boosts
0
Views
976
Activity
Sep ’22
Make a phone call programmatically.
Is there any way I can allow my app to programmatically make a phone call?
Replies
1
Boosts
0
Views
1.8k
Activity
Sep ’22
My iOS app in Shared with You
Is there a way for me to put my iOS app in side of the shared with you?
Replies
0
Boosts
0
Views
343
Activity
Sep ’22
How do I get my iOS app to run code when something specific in a message in Messages happens?
How do I get my iOS app to run code when something specific in a message in Messages happens, like a specific phone number sends a specific phrase?
Replies
2
Boosts
0
Views
1.1k
Activity
Sep ’22
How do I write thread-safe code that uses a completionHandler with a function that delegates code to an instance of OperationQueue?
I've been using the CloudKitShare sample code found here as a sample to help me write code for my app. I want to use performWriterBlock and performReaderBlockAndWait as found in BaseLocalCache using a completionHandler without violating the purposes of the design of the code, which focuses on being thread-safe. I include code from CloudKitShare below that are pertinent to my question. I include the comments that explain the code. I wrote comments to identify which code is mine. I would like to be able to use an escaping completionHandler if possible. Does using an escaping completionHandler still comply with principles of thread-safe code, or does it in any way violate the purpose of the design of this sample code to be thread-safe? If I use an escaping completionHandler, I would need to consider when the completionHandler actually runs relative to other code outside of the scope of the actual perform function that uses the BaseLocalCache perform block. I would for one thing need to be aware of what other code runs in my project between the time the method executes and the time operationQueue in BaseLocalCache actually executes the block of code and thus the completionHandler. class BaseLocalCache { // A CloudKit task can be a single operation (CKDatabaseOperation) // or multiple operations that you chain together. // Provide an operation queue to get more flexibility on CloudKit operation management. // lazy var operationQueue: OperationQueue = OperationQueue() // This sample ... // // This sample uses this dispatch queue to implement the following logics: // - It serializes Writer blocks. // - The reader block can be concurrent, but it needs to wait for the enqueued writer blocks to complete. // // To achieve that, this sample uses the following pattern: // - Use a concurrent queue, cacheQueue. // - Use cacheQueue.async(flags: .barrier) {} to execute writer blocks. // - Use cacheQueue.sync(){} to execute reader blocks. The queue is concurrent, // so reader blocks can be concurrent, unless any writer blocks are in the way. // Note that Writer blocks block the reader, so they need to be as small as possible. // private lazy var cacheQueue: DispatchQueue = { return DispatchQueue(label: "LocalCache", attributes: .concurrent) }() func performWriterBlock(_ writerBlock: @escaping () -> Void) { cacheQueue.async(flags: .barrier) { writerBlock() } } func performReaderBlockAndWait<T>(_ readerBlock: () -> T) -> T { return cacheQueue.sync { return readerBlock() } } } final class TopicLocalCache: BaseLocalCache { private var serverChangeToken: CKServerChangeToken? func setServerChangeToken(newToken: CKServerChangeToken?) { performWriterBlock { self.serverChangeToken = newToken } } func getServerChangeToken() -> CKServerChangeToken? { return performReaderBlockAndWait { return self.serverChangeToken } } // Trial: How to use escaping completionHandler? with a performWriterBlock func setServerChangeToken(newToken: CKServerChangeToken?, completionHandler: @escaping (Result<Void, Error>)->Void) { performWriterBlock { self.serverChangeToken = newToken completionHandler(.success(Void())) } } // Trial: How to use escaping completionHandler? with a performReaderBlockAndWait func getServerChangeToken(completionHandler: (Result<CKServerChangeToken, Error>)->Void) { performReaderBlockAndWait { if let serverChangeToken = self.serverChangeToken { completionHandler(.success(serverChangeToken)) } else { completionHandler(.failure(NSError(domain: "nil CKServerChangeToken", code: 0))) } } } }
Replies
0
Boosts
0
Views
747
Activity
Sep ’22
How many local CNContainers are on any single iOS device?
Is there always only one local CNContainer on any iOS device whether the user is logged in to iCloud or not, and whether the device is an iPhone or an iPad?
Replies
0
Boosts
0
Views
655
Activity
Sep ’22
What does DispatchQueue.AutoreleaseFrequency.workItem mean?
The documentation for DispatchQueue.AutoreleaseFrequency.workItem says , "The queue configures an autorelease pool before the execution of a block, and releases the objects in that pool after the block finishes executing." Does this mean that the dispatch queue releases each work item or each code in ()->Void after each one finishes executing? When it says, "releases the objects in that pool er the block finishes executing", it makes it sound like there is only one block executing and then the entire pool is released. I'm making sure I verify that I understand this correctly.
Replies
1
Boosts
0
Views
1.1k
Activity
Sep ’22
Where is the message instance property of MFMessageComposeViewController?
Why am I not seeing the message instance property of MFMessageComposeViewController? When I type the following, I get a code-time error message in Xcode that says "Value of type 'MFMessageComposeViewController' has no member 'message'" messageComposeViewController.message // error message: "Value of type 'MFMessageComposeViewController' has no member 'message'" There is nothing that explains this in the official Apple documentation: Documentation/Message UI/MFMessageComposeViewController/message
Replies
0
Boosts
0
Views
1.5k
Activity
Sep ’22
How do I create a perpetual "wizard" in iOS that allows the user to see the same options over and over until the user selects the option to end the wizard?
What is the most effective approach to to creating a type of "wizard" interface that takes the user through a process step-by-step, and allows the user to take the steps in any order as he goes along? For instance, the user could start out by selecting from three options: select contacts, select addresses, or selecting a message to send. When he selects one of those options, say select contacts, he sees the interface to allow him to perform that selection, and he sees the other two options to select at any time, which would be select addresses and select message. Say he then selects the option to select a message, he then sees an interface to selecta message and he also sees the other two options. And this continues indefinitely until he selects to send the message. I think the usual view controllers and segues would work for this. Would I need only one subclass of UINavigationController and three subclasses of UIViewControllers? Could this lead to any problems? Is it possible to do this with view controllers and segues? Is there a better way? I do use Swift, but this question doesn't require that I use only Swift, as you can see.
Replies
1
Boosts
0
Views
1k
Activity
Sep ’22