iosswiftnsuseractivityapp-search

iOS 9 App Search sendUserActivityToServer after it had been invalidated, so doing nothing


I want to index my notes for global search. I used a tutorial by Ray Wenderlich.

I added string into info.plist:

"myapp.com.notes.note"

Here is my object:

struct Note {

    public static let domainIdentifier = "myapp.com.notes.note"
    public var userActivityUserInfo:[String: Any] {
        return ["id": note_id]
    }
    public var userActivity: NSUserActivity {
        let activity = NSUserActivity(activityType: Note.domainIdentifier)
        activity.title = "\(note.title)"
        activity.userInfo = userActivityUserInfo
        activity.keywords = [note.title, note.text]

        return activity
    }
}

But I don't understand how to register it. In my view controller I tried:

override func viewDidLoad() {
   super.viewDidLoad()
   for note in self.notes {

      note.userActivity.becomeCurrent()
   }
}

But always get:

sendUserActivityToServer, called on activity EC1785C0-2904-4F0A-9885-F4DB91C4B245 after it had been invalidated, so doing nothing.


Solution

  • I haven't worked with AppSearch. But my guess is it can be related to object lifecycle.

    In the code you've posted there are no references to NSUserActivity object, so it is deallocated right after cycle body is executed.

    I believe the object should persist until resignCurrent is called on activity.

    And be vigilant, because Note is a struct in your code and it has a value semantic, so it is copied every time you deal with it.