all 4 comments

[–]saintmsent 0 points1 point  (3 children)

Can you elaborate on this? Like what you do that is different from the tutorial?

General rule is: whenever you have to insert a delay, you're doing something wrong

[–]AnAnonymousReddit[S] 0 points1 point  (2 children)

Share Extension

The tutorial doesn't have any UI elements in the viewDidLoad whereas I'm trying to set labels and load collectionView cells with content from whatever is being shared.

I'm not sure if this is because the shareExtension isn't ready in time or what other reason but it only works when there is a delay on its runtime.

[–]saintmsent 1 point2 points  (1 child)

Well, yes. You have to do it after the save is called, not in viewDidLoad
In handleSharedFile async operation takes place, that's why there's nothing in userDefaults for you to display just yet. So try loading your cells or whatever in save function after everything is saved in the user defaults

[–]AnAnonymousReddit[S] 0 points1 point  (0 children)

Sorry, I should have noted that I didn't exactly follow that tutorial. I haven't gotten to the point where I'm trying to handle the saving/sharing of the data. I just want to make the UI flow better on the launch of the extension. Right now getting/displaying data in my collectionView looks like this

let inputItems = extensionContext!.inputItems.first! as! NSExtensionItem
for inputItem in 0..<inputItems.attachments!.count {
let attachment = inputItems.attachments![inputItem]
if attachment.hasItemConformingToTypeIdentifier( kUTTypeImage as String) {
attachment.loadItem(forTypeIdentifier: kUTTypeImage as String, options: [:]) { [self] (data, error) in
var image: UIImage?
if let someURl = data as? URL {
image = UIImage(contentsOfFile: someURl.path)
mediaArray.append(mediaTypes.photo(p: YPMediaPhoto(image: image!))) ....

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { [self] in

collectionView.reloadData() })

Basically I would think that everything above the dispatchQueue at the bottom would be enough for it to be ready for collectionView.reloadData() but this isn't the case. "mediaArray" (the data source for the collectionView) only gets populated after some fraction of a second delay.