I'm having trouble successfully setting local data for Siesta in Swift. My goal is to set a UIImage for a URL locally, so that this local image can be displayed with no download time.
To do this, I'm setting the image data for the URL as such:
let resource = CustomRemoteImageView.imageCache.resource(myPhoto.url.absoluteString)
let imageData = UIImagePNGRepresentation(image)! // I've also tried putting the UIImage directly in there, because the transformation chain doesn't apply to local data, right?
let entity: Entity<Any> = Entity(content: imageData, contentType: "*/*") // I've played around with the content type too!
resource.overrideLocalData(with: entity)
I'm then using a custom Service that always tries to parse content as an Image:
private let imageTransformer =
ResponseContentTransformer
{ Image(data: $0.content)}
convenience init() {
self.init(standardTransformers: [])
configure {
$0.pipeline[PipelineStageKey.parsing].add(self.imageTransformer, contentTypes: ["*/*"])
}
}
This system is working great for all remote images, but it always seems to fail to parse this overridden local image. It seems like it's trying to parse but just fails every time.
i.e. I'm getting a Siesta.ResourceEvent of
(Siesta.ResourceEvent) $R20 = newData {
newData = network
}
but the actual .typedContent is nil.
overrideLocalDataandoverrideLocalContentdo not interact with the pipeline at all. Siesta won’t try to parse what you pass; what you override is what your resource gets.Furthermore,
overrideLocalDataandoverrideLocalContentdon’t fail. They always update the resource’s content. If you call those methods, the resource content will match what you passed.So … the problem isn’t parsing. What might it be?
Entity.typedContentis a shortcut for applyingas?to a resource’s entity’scontent. If you're getting nil, it means that either (1) thecontentof the entity you passed to theoverrideLocalDatawas nil or (2) the contextual type in which you’re callingtypedContentdoesn’t match thecontent’s actual runtime type.What do you see if you print
resource.latestData.content? That will show you what’s actually there, and will rule out type conversion issues withtypedContent.If it’s not nil, compare its value from a network request and get the types to match.
If it is nil, then either something else cleared the content or you passed nil content in the first place. Try
SiestaLog.Category.enabled = .commonand see if you can spot where it is or isn’t getting set to the right thing.