My build completes successfully. Then after some time this error pops up:
"Ambiguous use of 'addObjectsDidChangeNotificationObserver(handler:)'"
What I do not understand is why this happens because the addObjectsDidChangeNotificationObserver method is only declared once in the project and the second occurence shown by Xcode is the use of the method itself.
Here is the code where the error is shown and which Xcode also shows me as first candidate:
public init?(object: Managed, changeHandler: #escaping (ChangeType) -> ()) {
guard let moc = object.managedObjectContext else { return nil }
objectHasBeenDeleted = !type(of: object).defaultPredicate.evaluate(with: object)
token = moc.addObjectsDidChangeNotificationObserver(handler: {
[unowned self] note in
guard let changeType = self.changeType(of: object, in: note) else { return }
self.objectHasBeenDeleted = changeType == .delete
changeHandler(changeType)
})
}
and the implementation of addObjectsDidChangeNotificationObserver(), which Xcode shows me as second candidate:
extension NSManagedObjectContext {
public func addObjectsDidChangeNotificationObserver(handler: #escaping (ObjectsDidChangeNotification) -> ()) -> NSObjectProtocol {
let nc = NotificationCenter.default
return nc.addObserver(forName: .NSManagedObjectContextObjectsDidChange, object: self, queue: nil) { note in
let wrappedNote = ObjectsDidChangeNotification(note: note)
handler(wrappedNote)
}
}
}
Ok, the problem seems to be solved now.
Apparently, I messed up with the access modifiers, but good to know that something like that can cause an ambiguous error
Related
I have a function for getting user session. I successfully get the session. But inside the DispatchQueue i lose task object (task:AWSTask< AWSCognitoIdentityUserSession >).
Before DispatchQueue it is not null but in the dispatch DispatchQueue it is null. Some how i am losing the reference.
What is the best way to get and object reference from outside of the DispatchQueue. (I dont want to create a general variable in the class.)
BTW this is not happening all the time.
var pool:AWSCognitoIdentityUserPool
override init(){
pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
super.init()
}
func getUserPool() -> AWSCognitoIdentityUserPool {
return pool
}
func getUserSession(completition: #escaping () -> Void)
{
let user = pool.currentUser()!
let task = user.getSession()
task.continueWith{ (task:AWSTask<AWSCognitoIdentityUserSession>) in
{
DispatchQueue.main.async
{
if(task.result != nil && task.error == nil)
{
/*
There are some calculations here
*/
completition()
}
}
}
}
}
I'm using AWSAppSyncClient to upload files but I'm struggling to connect the upload progress hook with the view.
AWSAppSyncClient is a property of the the application delegate initialized with an S3ObjectManager. The object manager method upload has access to the upload progress via the AWSTransferUtilityUplaodExpression:
expression.progressBlock = {(task, progress) in
DispatchQueue.main.async(execute: {
// Can we update the controller's progress bar here?
print("Progress: \(Float(progress.fractionCompleted))")
})
}
My controller invokes the upload by calling perform:
var appSyncClient: AWSAppSyncClient? // retrieved from the app delegate singleton
appSyncClient?.perform(mutation: CreatePostMutation(input: input)) { (result, error) in ...
What I am struggling with: how do I provide the S3ObjectManager a reference to the controller? I thought of instantiating the AWSAppSyncClient in each controller, and maybe using some sort of delegate pattern?
It's probably overkill to instantiate a new client on each view controller. Setup & teardown take a bit of time & system resources to perform, and you'd probably prefer to keep those activities separate from the view controller in any case, just for separation of responsibilities.
There isn't really a good way of registering a per-object listener, since mutations are queued for eventual, asynchronous delivery. Your delegate idea seems like the best approach at this point.
NOTE: Code below is untested, and not thread-safe.
For example, you could declare a singleton delegate that manages watchers for individual views that need to report progress:
class AppSyncS3ObjectManagerProgressWatcher {
typealias ProgressSubscription = UUID
static let shared = AppSyncS3ObjectManagerProgressWatcher()
private var watchers = [UUID: AppSyncS3ObjectManagerProgressDelegate?]()
func add(_ watcher: AppSyncS3ObjectManagerProgressDelegate) -> ProgressSubscription {
let subscription = UUID()
weak var weakWatcher = watcher
watchers[subscription] = weakWatcher
return subscription
}
func remove(_ subscription: ProgressSubscription?) {
guard let subscription = subscription else {
return
}
watchers[subscription] = nil
}
}
extension AppSyncS3ObjectManagerProgressWatcher: AppSyncS3ObjectManagerProgressDelegate {
func progressReportingExpression(forDownloadingObject object: AWSS3ObjectProtocol) -> AWSS3TransferUtilityDownloadExpression {
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = { _, progress in
self.didReportProgress(forDownloadingObject: object, progress: progress)
}
return expression
}
func progressReportingExpression(forUploadingObject object: AWSS3ObjectProtocol & AWSS3InputObjectProtocol) -> AWSS3TransferUtilityUploadExpression {
let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = { _, progress in
self.didReportProgress(forUploadingObject: object, progress: progress)
}
return expression
}
func didReportProgress(forDownloadingObject object: AWSS3ObjectProtocol, progress: Progress) {
for watcher in watchers.values {
watcher?.didReportProgress(forDownloadingObject: object, progress: progress)
}
}
func didReportProgress(forUploadingObject object: AWSS3ObjectProtocol & AWSS3InputObjectProtocol, progress: Progress) {
for watcher in watchers.values {
watcher?.didReportProgress(forUploadingObject: object, progress: progress)
}
}
}
Wherever you conform S3TransferUtility to S3ObjectManager, you would do something like:
extension AWSS3TransferUtility: AWSS3ObjectManager {
public func download(s3Object: AWSS3ObjectProtocol, toURL: URL, completion: #escaping ((Bool, Error?) -> Void)) {
let completionBlock: AWSS3TransferUtilityDownloadCompletionHandlerBlock = { task, url, data, error -> Void in
if let _ = error {
completion(false, error)
} else {
completion(true, nil)
}
}
let progressReportingExpression = AppSyncS3ObjectManagerProgressWatcher
.shared
.progressReportingExpression(forDownloadingObject: s3Object)
let _ = self.download(
to: toURL,
bucket: s3Object.getBucketName(),
key: s3Object.getKeyName(),
expression: progressReportingExpression,
completionHandler: completionBlock)
}
public func upload(s3Object: AWSS3ObjectProtocol & AWSS3InputObjectProtocol, completion: #escaping ((_ success: Bool, _ error: Error?) -> Void)) {
let completionBlock : AWSS3TransferUtilityUploadCompletionHandlerBlock = { task, error -> Void in
if let _ = error {
completion(false, error)
} else {
completion(true, nil)
}
}
let progressReportingExpression = AppSyncS3ObjectManagerProgressWatcher
.shared
.progressReportingExpression(forUploadingObject: s3Object)
let _ = self.uploadFile(
s3Object.getLocalSourceFileURL()!,
bucket: s3Object.getBucketName(),
key: s3Object.getKeyName(),
contentType: s3Object.getMimeType(),
expression: progressReportingExpression,
completionHandler: completionBlock
).continueWith { (task) -> Any? in
if let err = task.error {
completion(false, err)
}
return nil
}
}
}
And then in the progress reporting view:
override func awakeFromNib() {
super.awakeFromNib()
progressSubscription = AppSyncS3ObjectManagerProgressWatcher.shared.add(self)
}
func didReportProgress(forUploadingObject object: AWSS3InputObjectProtocol & AWSS3ObjectProtocol, progress: Progress) {
// TODO: Filter by object local URI/key/etc to ensure we're updating the correct progress
print("Progress received for \(object.getKeyName()): \(progress.fractionCompleted)")
self.progress = progress
}
As I noted, this code is untested, but it should outline a general approach for you to start from. I'd welcome your feedback and would like to hear what approach you eventually settle on.
Finally, please feel free to open a feature request on our issues page: https://github.com/awslabs/aws-mobile-appsync-sdk-ios/issues
I have a problem with working set enumerator (Recents tab) in Files app. I implement a working enumerator for Folders, it runs enumerateItems(for observer method when I move to the folder in UI and everything works fine.
And I have a different enumerator for working set too, but it is not created and enumerated anything.
override func enumerator(for containerItemIdentifier: NSFileProviderItemIdentifier) throws -> NSFileProviderEnumerator {
let maybeEnumerator: NSFileProviderEnumerator?
if (containerItemIdentifier == NSFileProviderItemIdentifier.rootContainer) {
let directory = repository.directory(for: containerItemIdentifier)!
maybeEnumerator = FileProviderFolderEnumerator()
} else if (containerItemIdentifier == NSFileProviderItemIdentifier.workingSet) {
maybeEnumerator = FileProviderWorkingSetEnumerator() // not called
} else {
_ = repository.item(for: containerItemIdentifier)
if repository.isDirectory(at: containerItemIdentifier) {
let directory = repository.directory(for: containerItemIdentifier)!
maybeEnumerator = FileProviderFolderEnumerator() // works fine
} else {
maybeEnumerator = FileProviderItemEnumerator()
}
}
guard let enumerator = maybeEnumerator else {
throw NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo:[:])
}
return enumerator
}
I'm trying to call working set enumerator in startProvidingItem method using signal, but it doesn't work.
override func startProvidingItem(at url: URL, completionHandler: #escaping ((_ error: Error?) -> Void)) {
loadItem {
//when done with loading
NSFileProviderManager.default.signalEnumerator(
for: NSFileProviderItemIdentifier.workingSet,
completionHandler: { error in
if let err = error { print(err) }
}
}
}
Does FileProviderWorkingSetEnumerator initialize automatically when I open Recents tab in Files app? Should I call it somehow directly from FileExtension?
Thank you!
The working set enumerator doesn't work like what you think.
The file provider extension will enumerate working set in background, even before you open Files app.
That's why you need to prepare working set data in offline.
The WWDC 2017 document might help.
Okay, so I'm trying to build an iOS app that relies on Firebase (To work with its android version)
I started with creating a repository for each actor in my app and a general repository to manage them all
Each repository manages the observers of this actor. An example:
Inside the PagesRepository, this is a function that retrieves all the pages from Firebase and returns it inside a completionHandler:
//MARK: Gets the whole pages list
func getPagesList(completionHandler: #escaping (_ pages: [Page]?, _ error: NSError?) -> Void) {
func displayError(error: String) {
print(error)
completionHandler(nil, self.getErrorFromString(error))
}
pagesRef.observe(DataEventType.value) { pagesSnapshot in
guard pagesSnapshot.exists() else {
displayError(error: "Pages snapshot doesn't exist")
return
}
var pagesList = [Page]()
for pageSnapshot in pagesSnapshot.children {
pagesList.append(Page(snapshot: pageSnapshot as! DataSnapshot))
}
completionHandler(pagesList, nil)
}
}
And then I call it from the ViewController like this:
repository.getPagesList { (pages, error) in
guard error == nil else {
return
}
//Do processing
}
I know this may be a lot to take in, but my problem is that every time I call the function, it creates a new observer but doesn't cancel the old one... So, the completionHandler is called multiple times with different values
How should I manage this problem?
(Sorry for being complicated and a little unclear, I'm just really lost)
It seems like you only want to observe the value once so I would use this instead:
func getPagesList(completionHandler: #escaping (_ pages: [Page]?, _ error: NSError?) -> Void) {
func displayError(error: String) {
print(error)
completionHandler(nil, self.getErrorFromString(error))
}
pagesRef.observeSingleEvent(of: .value, with: { (pagesSnapshot) in
var pagesList = [Page]()
for pageSnapshot in pagesSnapshot.children {
pagesList.append(Page(snapshot: pageSnapshot as! DataSnapshot))
}
completionHandler(pagesList, nil)
}) { (error) in
// Display error
}
}
So, I created a typealias to store a completion handler, to later pass into a function called submitTokenToBackend using Stripe's iOS library. Here is my code:
// MARK: - Create Completion Handlers
typealias CompletionHandler = (_ token: AnyObject?, _ error: NSError?) -> Void
// MARK: - Submit Token To Backend
func submitTokenToBackend(completionHandler: CompletionHandler) {
}
// MARK: - STPPaymentCardTextFieldDelegate
func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) {
print("Card number: \(textField.cardParams.number) Exp Month: \(textField.cardParams.expMonth) Exp Year: \(textField.cardParams.expYear) CVC: \(textField.cardParams.cvc)")
self.buyButton.isEnabled = textField.isValid
}
// MARK: Initialize Card Params
let cardParams = STPCardParams()
func cardParamsFunc() {
cardParams.number = "4242424242424242"
cardParams.expMonth = 10
cardParams.expYear = 2018
cardParams.cvc = "123"
STPAPIClient.shared().createToken(withCard: cardParams){ (token, error) in
if let error = error {
print(error.localizedDescription)
} else if let token = token {
// HERE'S WHERE I'M GETTING ERRORS
self.submitTokenToBackend(completionHandler: CompletionHandler) -> Void {
if let error = error {
print(error.localizedDescription)
} else {
print("Show receipt page")
}
}
}
}
}
I am getting these weird errors, now, in Swift 3 concerning my completion handler not having expected types. Not an isolated incident, either. Any thoughts?
Almost all thing you need is described in Rob Napier's answer.
I'll try to show you a little more concrete code...
You can define the completion handler and pass it to submitTokenToBackend(completionHandler:) like this:
let theCompletionHandler: CompletionHandler = {token, error in
if let error = error {
print(error.localizedDescription)
} else {
print("Show receipt page")
}
}
self.submitTokenToBackend(completionHandler: theCompletionHandler)
With removing intermediate let-constant, you can write it in this way:
self.submitTokenToBackend(completionHandler: {token, error in
if let error = error {
print(error.localizedDescription)
} else {
print("Show receipt page")
}
})
Using the trailing closure feature of Swift, the above code can be shortened to:
self.submitTokenToBackend {token, error in
if let error = error {
print(error.localizedDescription)
} else {
print("Show receipt page")
}
}
Your code is far from any of above three.
self.submitTokenToBackend(completionHandler: CompletionHandler) -> Void {
This is a declaration, not a method call. You can't pass a typealias as a parameter. And -> Void does not make sense here at all. You almost certainly meant
self.submitTokenToBackend {
If you have further questions on this, however, you need to provide code we can compile (see mcve) and list the exact errors. "Weird errors" is not particularly helpful for debugging.
Im not sure, but... Where is 'CompletionHandler' implementation? As I understand, you just declarate some like block in Objective - C ('typedef void (^completionHandler)(id token);'), but don't use it.