0

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.

MQLN
  • 2,292
  • 2
  • 18
  • 33

1 Answers1

2

overrideLocalData and overrideLocalContent do 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, overrideLocalData and overrideLocalContent don’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.typedContent is a shortcut for applying as? to a resource’s entity’s content. If you're getting nil, it means that either (1) the content of the entity you passed to the overrideLocalData was nil or (2) the contextual type in which you’re calling typedContent doesn’t match the content’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 with typedContent.

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 = .common and see if you can spot where it is or isn’t getting set to the right thing.

Paul Cantrell
  • 9,175
  • 2
  • 40
  • 48