Stack Overflow When Defining Subscript on Ckrecord in Swift

Stack overflow when defining subscript on CKRecord in Swift

After some testing and debugging (via a subclass), I discovered that, for CKRecord, objectForKey: does indeed call objectForKeyedSubscript:. Also, implementing subscript in a Swift class that is marked @objc implicitly (by descending from NSObject) or explicitly means that subscript is implemented as objectForKeyedSubscript:.

This means that implementing subscript on CKRecord in an extension hides the default implementation, which causes the stack overflow.

Does subscript operator work for CKRecord?

On this post you can see an explanation why subscripting CKRecord does not work and why you also can not add your own.

Stack overflow when defining subscript on CKRecord in Swift

There is however a sample of a workaround that comes close.

Check if record subscription already exists in CloudKit

You can use fetchAllSubscriptionsWithCompletionHandler to query all existing subs, then you can check subscriptionID property on each sub returned in the completion handler. The objective-c version looks like:

    [publicDatabase fetchAllSubscriptionsWithCompletionHandler:^(NSArray<CKSubscription *> * _Nullable subscriptions, NSError * _Nullable error)
{
NSMutableArray *subIDs = [NSMutableArray new];
for (CKSubscription *sub in subscriptions)
{
if ([sub.subscriptionID isEqualToString:@"whatever"];
{
//do some stuff
}
}
}];

However, you mention re-entering a view controller and re-running this check. Not that this check issues a request to the server each time and will thus count against your transaction and transfer quotas. I recommend instead that you run this check once on app startup, and then save the state of each sub in class variables so you don't have to keep re-querying the server repeatedly with unnecessary calls.

Type 'Any' has no subscript members after updating to Swift 3

In FIRDataSnapshot, value is of type id.

In Swift 3, id is imported as Any.

In the Firebase documentation, it says value can be any of NSDictionary, NSArray, NSNumber, or NSString -- clearly, subscripting doesn't make sense on all of these, especially in Swift. If you know it's an NSDictionary in your case, then you should cast it to that.



Related Topics



Leave a reply



Submit