Skip to content

Commit

Permalink
Refactor search to make a little more sense w/ main thread limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
NattyNarwhal committed Sep 17, 2023
1 parent c283384 commit 56d1b24
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
17 changes: 12 additions & 5 deletions Submariner/SBSearchResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@
import Cocoa

@objc class SBSearchResult: NSObject {
/// Used for bindings and contains the actual tracks fetched from `fetchTracks:`.
@objc var tracks: [SBTrack] = []
@objc let query: String // NSString

// FIXME: Nasty hack for SBServerSearchController since the tracks were sourced from a different thread
// it should be an array of IDs and the UI thread fetches that
@objc func replaceManagedInstancesForThread(managedObjectContext: NSManagedObjectContext) {
tracks = tracks.map { track in
managedObjectContext.object(with: track.objectID) as! SBTrack
/// Contains the list of tracks to fetch on the main thread, and fills `tracks` from that.
///
/// This can be appended to.
var tracksToFetch: [NSManagedObjectID] = []

/// Updates the tracks array after getting the results.
///
/// This has to be done on the main thread, as the parse operation that builds the list runs off the main thread.
@objc func fetchTracks(managedObjectContext: NSManagedObjectContext) {
tracks = tracksToFetch.map { trackID in
managedObjectContext.object(with: trackID) as! SBTrack
}
}

Expand Down
2 changes: 1 addition & 1 deletion Submariner/SBServerSearchController.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ - (void)subsonicSearchResultUpdatedNotification:(NSNotification *)notification {
SBSearchResult *results = [notification object];
// HACK: We can't use the objects that were fetched on another thread.
// Refetch on this one from ours. Better way possible?
[results replaceManagedInstancesForThreadWithManagedObjectContext: self.managedObjectContext];
[results fetchTracksWithManagedObjectContext: self.managedObjectContext];
[self setSearchResult:[notification object]];
});
}
Expand Down
4 changes: 2 additions & 2 deletions Submariner/SBSubsonicParsingOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,12 @@ class SBSubsonicParsingOperation: SBOperation, XMLParserDelegate {
// the song element has the same format as the one used in nowPlaying, complete with artist name without ID
updateTrackDependenciesForDirectoryIndex(track, attributeDict: attributeDict)
// objc version did some check in playlist, which didn't make sense
currentSearch.tracks.append(track)
currentSearch.tracksToFetch.append(track.objectID)
} else {
logger.info("Creating track ID \(id, privacy: .public) for search")
let track = createTrack(attributes: attributeDict)
updateTrackDependenciesForDirectoryIndex(track, attributeDict: attributeDict)
currentSearch.tracks.append(track)
currentSearch.tracksToFetch.append(track.objectID)
}
} else {
logger.warning("Current search was null on a song element")
Expand Down

0 comments on commit 56d1b24

Please sign in to comment.