I’m constructing a feed with hyperlinks to multimedia property. If a hyperlink is unhealthy it shouldn’t be within the feed as a video that doesn’t play or picture that doesn’t load is a poor person expertise. The feed is pulled by the cellphone by way of JSON from an endpoint with out the good thing about a websocket.
I can check the hyperlinks earlier than placing them within the feed, nonetheless, ready to confirm a hyperlink takes time and if the feed has many gadgets, I am anticipating gradual loading instances.
Even accepting the delay for validating hyperlinks, I’m combating tips on how to implement asynchronous calls to the hyperlinks inside a synchronous conversion course of as soon as we’ve got downloaded the information.
Right here is the workflow.
-
Asyncchronously import JSON as object. You get a JSON object equivalent to:
{“feed”:[{“title”:”Check out this video”,”videosurl”:”https://www.youtube.com/somevideo.mp4″}]}
-
Serialize JSON into Array of feed objects (synchronous)
-
Save the array of Feed objects to Core Knowledge (synchronous)
-
Show the objects utilizing an NSFetchedResultsController. (synchronous)
Inside 3-just earlier than saving every merchandise to Core Knowledge would appear like a logical place to check/validate the hyperlinks.
Right here is my code to this point within Step 3
-(void) saveFeed:(NSArray *)gadgets inContext: (NSManagedObjectContext*) context
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Videofeed" inManagedObjectContext:context];
gadgets* merchandise;
for (i=0;i
@objc public extension NSString {
@out there(iOS 13.0.0, *)
func fileExists(at surl: NSString) async throws -> Bool {
let str = self as String
guard let url = URL(string: str) else { return false }
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
request.timeoutInterval = 1.0 // Modify to your wants
let (_, response) = strive await URLSession.shared.information(for: request)
return (response as? HTTPURLResponse)?.statusCode == 200
}
How can I get trigger the synchronous a part of this workflow to correctly pause for the asynchronous checks of the file validity or is there a greater strategy? Thanks upfront for any options.