Say we have a User class with properties id: Int, name: String, imageUrl: String.
Our imaginary API stores the URL to the image. Is there a way to fetch this image once, and then reuse it in many places across the app as if it were another variable like name? I'm basically trying to be able to call user.image as a property after I fetch it.
I have no issues asynchronously fetching the image, but I can't figure out how I can only fetch it once, store it somewhere, and reuse it as a class variable (or at least something like it, just as simply), instead of fetching it every time I want to use it.
I'm sure many people deal with a similar type of situation where APIs return URLs to images. Is there an efficient way to handle this?
You could probably implement this yourself but I'd recommend Kingfisher instead. Kingfisher does exactly what you're asking for, it caches images so you only need to get them from the server once.
Related
I'm working on an app that needs to be able to upload an array (can contain a mix of UIImage or a custom struct that contains a local file URL and some data). I've seen Operations and OperationQueues as a possible starting point but I don't know if I'm looking in the right direction.
Hopefully, this image can provide some clarity regarding what I need to accomplish. Basically, each ProgressViewController can have its own "uploadable" array and I also need to track the progress for each upload. I know I need to use the URLSessionDelegate methods for tracking progress, but are Operations, OperationQueues and a singleton "UploadManager" of sorts the way to go?
Thanks for your help guys!
Recently, I'm working with a timetable app in iOS, and i get trouble with Core Data.
My app has a main user interface kind of like the original calendar app created by Apple, and i save all my events data in the Core Data database.
I create a UIManagedDocument in order to fetch data from database by using its NSManagedObjectContext, and everything works just fine.
However, i need to use the NSManagedObjectContext to fetch data several times in several different view controllers during the runtime. And every time i do this, i need to reopen the UIManagedDocument, but open the document take too much time(it may take 2 seconds or even more, i have to display a spinner in view).
So here are my questions:
What's the right way to open a managedDocument?(I mean like open it during the lunch image time?)
Is there a way to only open the managedDocument once and keep it open during runtime?(So i can use its context all the time)
Does data store in the managedDocument i create?(I found that if i delete the document, data was gone)
Thanks.
You will get lots of different opinions on how to do this but basically you should only have to open the document once. This is done by some object that does the work and stores it so it can return it again when asked by a different view controller.
Many people create singleton's for this or put it in the App Delegate. I have implemented a protocol that lets me put it where ever it is convenient for a particular application without my other code having to know anything about the object that returns the information. As long as it responds to the protocol it can be the App Delegate, a singleton class, or any other object type.
See importing AppDelegate
The protocol that I put in the above example just returns information about where the database is. In my actual implementation I have an openDatabase method with a call back to let me know when it is done plus automatic initialization and updating methods.
You can also improve your performance by having the open operation happen off the main thread. This keeps your UI responsive but does not show the data any faster and you have to be carefull about managed object contexts and the threads they are in.
Good luck...
I have an application which must save client instances of com.smartgwt.client.widgets.Canvas to a disk on the server, and then restore them.
My solution is to serialize the canvases, send them to the server to be saved, save them, load them, send them back to the client, and then deserialize.
Currently, the network transfer code is all in place. The only thing left is serializing the canvases. However, I want to know if doing so is even possible? If not, any workarounds? Clues?
Thanks,
Ian
I would suggest that you do not serialize the Canvases themselves, but instead save their state. Depending on what the canvases contain, this can be easier or harder, but in most cases makes better sense than trying to save the objects themselves. SmartGWT provides API calls to save the state of some complex object, e.g. ListGrids . For simpler objects you can come up with a way to store their view state, e.g by using a JSON object that holds of what is important to your case.
I'm just starting off on my first attempt at the MVP architecture in GWT.
My understanding is that the job of PlaceTokenizer.getPlace(String) is to take the token in the URL after the hash, parse it, and return a Place with the object to which that token refers.
In my application, I have a resource (say, a video), which I tokenize by the video's unique id. Then my getPlace method ought to take this id, and retrieve the video information from the server. Is this the right approach?
As far as how to execute this, the only way that I could figure out would be to have my RemoteService right on the PlaceTokenizer, and make the call right in that getPlace method. Something about this made me hesitate. It seems like the call would be better suited for the Activity or somewhere else.
Is this how it's done? If not, is there a better way?
Your Place doesn't need to download the video. It could just contain the video's id.
Your Activity will receive the Place, which has the video id, and now the Activity can do the downloading or whatever heavy lifting you want.
So: Your PlaceTokenizer only needs to know enough to store the video id in the Place. Let the Activity do the work after that. The only function of getPlace is to turn a String into a Place.
It helped me to mentally rename Place to PlaceTag. The place objects really do not represent places - they represent tags or pointers to places. The actual place is represented by, terribly, the Activity.
if i may help clarifying the place signification a little more. Your place object is a serializable representation of the state of your client. it will help the activity restoring the screen to its former state by containing all the information you need to build it back.
I've not found a answer to this question anywhere, but this seems like a typical problem:
I would like to send some POST-Requests (with ASIHTTPRequest, what I already do), but if something goes wrong, ther user can decide to "Try Later", that means, the task should be put on a queue and this queue should be read next time the application starts. So, that's my question: how to "save" the queue, so that the app can read it next time it starts? Is it possible to "read" the queue and try sending this POST-Request again, let's say, 10 min later, even if the application is not running?
What kind of documentation should I read in order to be able to do this?
I would be very glad to hear any answers. Thanks in advance.
P.S.: Another Idea I have: as I just have to Upload Photos, I could have a folder with all the Photos that still need to be uploaded, and when the App starts, the app looks at this folder and try to send all the photos in this folder. Does it make sense?
My approach for this issue would be like this:
Whenever you fail to send details - write content of the array to a file using '[NSArray writeToFile:]' you can use serialization if array contain any data which is custom defined (if your array contain standard cocoa objects(NSString,NSData etc) they already implemented with serialization )
When app launches; load the content from file directly to an array object ('[NSArray arrayWithContentsOfFile:]')
then construct http request and try sending. In application the data(in your case array) is stored/serialized not the request, you need to reconstruct the http request when you want to try one more time.(don't try serializing ASIHTTPRequest, you have reconstruct it)
I'm going to assume you've already looked at NSOperationQueue and NSOperation. AFAIK there is no built-in support for serializing NSOperation, but you could very easily write your own serialization mechanism for an NSOperation subclass that you use for posting data and write the an NSOperationQueue's operations to disk if something goes wrong.
Without knowing too many details it's hard to give a precise answer. There are many ways to write data to disk and load it again later, the direction you take will be largely dependent on your situation.