Hi all,
I’m trying to use NSMetadataQuery on iOS to track changes to folders users have imported from elsewhere but, no matter what I try, I get no results.
Following the documentation for searching file metadata with NSMetadataQuery,
I’m creating a live query (albeit in Swift) and listening for […]QueryDidFinishGathering and […]QueryDidUpdate. The former fires, with no results, and the latter never fires.
I’ve also tried following the Synchronizing Documents in the iCloud Environment example, adding the appropriate Ubiquity keys to my Info.plist and .entitlements file, with no change.
I’m importing files and folders using SwiftUI’s View.fileImporter(isPresented:allowedContentTypes:allowsMultipleSelection:onCompletion:), but can’t see how I might security-scope the NSMetadataQuery’s execution (if that’s even a thing?).
My test project is on GitHub, but the main parts are below…
My query method:
extension NSMetadataQueryUbiquitousExternalDocumentsTestApp {
func findAccessibleFiles() {
query.stop()
fileMonitor?.cancel()
fileMonitor = Publishers.MergeMany(
[
.NSMetadataQueryDidFinishGathering,
.NSMetadataQueryDidUpdate
].map { NotificationCenter.default.publisher(for: $0) }
)
.receive(on: DispatchQueue.main)
.sink { notification in
query.disableUpdates()
defer { query.enableUpdates() }
foundItems = query.results as! [NSMetadataItem]
print("Query posted \(notification.name.rawValue) with results: \(query.results)")
}
query.searchScopes = [
NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope
]
query.predicate = NSPredicate(
format: "%K LIKE %@",
argumentArray: [NSMetadataItemFSNameKey, "*"]
)
query.sortDescriptors = [
NSSortDescriptor(key: NSMetadataItemFSNameKey, ascending: true)
]
if query.start() {
print("Query started")
} else {
print("Query didn't start for some reason")
}
}
}
Info.plist:
[…]
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.stevemarshall.AnnotateML</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>AnnotateML</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>ANY</string>
</dict>
</dict>
[…]
Post
Replies
Boosts
Views
Activity
Hi all,
I'm trying to use NSPersistentCloudKitContainer.initializeCloudKitSchema() to initialise a public CloudKit database, but it seems to fail when any given entity has more than two String fields (or, it seems, any two fields which NSPersistentCloudKitContainer decides need to be CKAsset-backed).
I've raised rdar://FB8995024 about this too, but was wondering if someone here can see something I'm missing.
I can consistently reproduce this on iOS 14.4 (18D46) on the iOS Simulator with Xcode 12.4 (12D4e), or with an iPhone 11 Pro running iOS 14.4 (18D52) using this code (in a new iOS App project, SwiftUI/SwiftUI App/Swift, Use Core Data+Host in CloudKit) and a single Core Data entity with two String attributes:
import CoreData
import SwiftUI
@main
struct NSPersistentCloudKitContainer_initTestingApp: App {
let persistenceController = PersistenceController.shared
var body: some Scene {
WindowGroup {
Text("Success")
}
}
}
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init() {
container = NSPersistentCloudKitContainer(name: "NSPersistentCloudKitContainer_initTesting")
				// If we change this to .private, everything is fine
container.persistentStoreDescriptions[0].cloudKitContainerOptions?.databaseScope = .public
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
do {
try container.initializeCloudKitSchema()
} catch {
fatalError("###\(#function) Error initialising CloudKit schema: \(String(describing: error))")
}
}
}
Thanks!