last day (15 days later) » 

6:38 PM
0
A: Buggy and Slow scrolling when loading TableView images from CoreData

WainYou shouldn't always load the image from disk, it's slow. Instead, load the image from disk the first time and then store it in a cache (in memory). Check if the image is cached before loading it from disk. You need to empty the cache if it gets too big or you get a memory warning. Advanced ver...

 
sounds interesting , can you please support an example with my method above? :(
 
give it a try, you learn by thinking through and solving the problem ;-)
 
tried for the last 3 hours.. no results , i think i found the problem. each time i scroll it re-load it from the local directory , its suppose to be like that?
 
Thats why I talk about caching once loaded from disk
 
so you mean everytime i load it move it to nscache?
 
6:38 PM
yes, but that means you only load it once, then after that you get from the cache (only load if the cache is empty for that image)
 
I'll try that ,, so in other words the slowleness comes because the load of the image from cache directory?
 
Yes, because you have to read data from disk and that's slow
 
Thats so frustrating , i cant find good explanation how to store and get data with NSCache using swfit lol , do you have a good resource?
 
No, I don't really write swift at the moment. It doesn't need to be an NSCache though, an NSDictionary will do just fine with the key as the path and the value as the image
 
Wain i really cant understand how saving something locally and than load it is slower than use NSDictionary , FB app save all the data locally , how the hell do they parse it so fast?
 
6:39 PM
how do you mean? loading an image from an NSDictionary is orders of magnitude faster than loading from disk
 
Hey
Here? :)
@Wain
 
> how do you mean? loading an image from an NSDictionary is orders of magnitude faster than loading from disk
 
First of all hello and thank you for your time
i will accept your answer of course :)
secondly , im really frustrating
Did you ever use Instagram or have instrgram by chance?
or seen how they work?
 
i don't, no
 
Hm k , so generally
 
6:51 PM
most apps will download data from the web, store it on disk for offline capability and memory management but display data from memory
they effectively cache the stuff they need at the moment in memory
 
So this is the right sequence :
 
Core Data is doing this behind the scenes for you, just not for images
 
Server > Download > Store locally > Load to cache > load to UI?
im storing in core only the names of the images
and than take it from there and load the img by url
 
it depends if this is the first time or not, if you have the data in CD then you may not need to download fresh
and after the download you have it in memory, so really its:
Server > Download > Unpack > Store locally (to disk) > load to UI?
 
whats Unpack?
 
6:54 PM
for non-image data anyway, as the unpacking is of the path
unpack is parse the JSON for example
 
oh get it
and is it correct to say, that ill be stroing only Data like images etc, on the disk
 
well, the CD SQLite file is stored to disk, holding the image paths
 
and i can rely on that , that the Data (etc json from the server) is very small bytes and its okay to re-download it everytime?
 
and your images are stored to disk
redownloading should be about whether you need to refresh, not about the size
 
only texts stuff like names description so on
please explain
:X
 
6:56 PM
if you have a DB of names on the server for example
and you don't allow them to be changed
and you expect new names only once per week
then downloading every day is pointless
but if you expect them to change every 5 minutes then downloading each day makes sense
maybe even more often, probably on explicit user request
there is no 1 rule for all situations...
 
oh well thats a nice thought
Wain i dont mean to be rude , but there is any chance that you have spare 15min to help me?
I want to show you via screen share what is the sequence im taking
usually i dont need help
but this area his very difficult lol
 
feel free to write your question here, but I can't screen share from here
 
hm k , i just want to check if its effective or not
because i really feel like its not
I hope you'll get it
Viewdidload has a dataTask method that gets all the Json
i convert the json into array(inside the task completion handler) and run a for loop for each item
for each item im checking (inside the for) if he is in the Core Data or not
 
ok (the view controller isn't necessarily the best owner, but fine)
 
if he is im appending the values to the uitableview datasource
lets assume that for the first part , im not downloading anything from the internet (already downloaded) and all my files are locally
than after he finish the for loop im doing reload tableview
thats the main cincept
concept
 
7:02 PM
you may not have time for such a change, but using something like RestKit and NSFetchedResultsController will likely help you a lot
sounds fine so far
 
im using NSFetchresukt
result
this is the page im getting info from roimulia.com/db.php
and in the tableviewdelegate im doing this
 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as? UITableViewCell
 let photoDetails = photos[indexPath.row]


    cell!.textLabel.text = photoDetails.name as String




      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
        var myPathList : NSArray = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask,
which i know is not good but i think its the direction
Please , i know self-learning is the best kind
but im 1 week already on this t hing
and it really frustrating
 
it's close
you have the check now so you don't load the image to the wrong thread
 
but its working very slowly
can i send you the prject maybe>
project*
and see what may cause the problem?
 
after cell!.textLabel.text you now want to check a cache dictionary for the image before you decide if you need to do the dispatch_async
the image display is slow, or the scrolling is lag?
 
i need to create the cache as global theard?
scrolling is laggy
global parameter*
 
7:06 PM
this code should just be slow image loading, not actually laggy scrolling
cache should be an instance variable in the view controller
 
I added the images locally
and it worked perefectly
so the images arent the problem
i think like you said , its the loading
can we do it together?
I will be greatfull , im trying to understand honsetly
i made a few apps already , but this one really kicking me
 
added them locally? you mean displayed images from disk only and there was no lag?
 
like i dragged the image to the xcode project
and than setted the number of rows to 40
and i did that the image of the tableview is uiimage(named : "lol.png")
and it worked fine
so the image size isnt the problem
 
uiimage(named: ...) has a private cache behind the scenes...
 
means?
:D
Wain how do you know so much about this?
 
7:12 PM
it means it doesn't load the image from disk each time, just once
 
are you proffesor or something?
 
define professor...
i've been doing it for years
 
you have degress?
degree
 
not in computer science, but yes
 
oh lol thats awesome
:)
so back to out buissnes
what are you recomendation for our seneario?
if i understand you correcly
if i will add the values to nscache in the process of the DataTask
and than check in the delegate method if it exists
and load it from nscache
it suppose to work smoothly?
or im missing something?
 
7:15 PM
yes, if you run the download each time then this will work
if you load the table with local data before the download completes then it would be better to add the images to the cache then too
and check the cache for the specified image both in the table view and the download code
 
well im doing reload data once it finished
so i dont know if its good
can you help me write it? please please please!! :X
 
you're just missing the cache
I don't really write swift at the moment so you know the syntax better than me
create a dictionary instance variable
check if it holds anything for the image path
if it does, use it
if not, load the image and add it to the cache
 
but for checking if the directory has a value
i need to run inside an for statemnt
statment
and its very not efficeint
i meani have nsdirectory
thats old the id 5 with the img
but i have 200 values inside
i need to run a for loop that checks where is the value
thats very not efficient isnt it?
 
which value are you referring to ?
 
so when i download the images online
i save the id from the json
and to make easier life for me , when im saving the images locally i name it as the id
 
7:22 PM
ok, so what is the loop for ?
 
so if im downloiad the img from the url , and the image match id 233 , ill save the images locally as 223.png
 
yes
 
you said check if the directory empty , and if not load the img from there
but if i have alot of values inside , i need to run a loop for to find the correct value thats match the id
 
dictionary, not directory
 
so the nsdictoniery will look like this : ["ID" : 5 , "img" : UIImage]
ya i ment dicitioory
dictionary
 
7:25 PM
yes
and when you have the 'id' you access the dictionary directly
no loop
 
is it log(1)?
or log (n)
 
as I say, I don't have a comp-sci degree, and Apple don't tell you exactly how the dictionary is implemented
it's some form of hash map
I don't remember the O for hash maps
 
but you think its efficeint?
 
certainly
 
its yes or no? im not famillier with certainly word lol
 
7:28 PM
yes
 
oh k
but why use nsdictionary and not nscache?
 
either will work
dictionary gives you full control
cache does some emptying for you, allows you to specify some options
dictionary is 'simple', cache is 'a bit complex'
 
ya but isnt it more optimized for fast fetching?
 
and dictionary is easier to do for you as you can find swift sample code
they are likely about the same
the difference is small for your usage
compared to loading from disk you won't notice the difference between dictionary and cache
 
kk
so lets do that
so i need to declare global dictionary , and should i add values to him in the for loop right?
 
7:35 PM
the loop where you process the JSON ?
 
yes
is it the right way?
 
yes
 
alright
so 1 sec ill try
and i store like 2 values , 1 of the id
and second the image data?
like the path or the images itself?
 
the key is the id, which is the file name
the value is the image
 
oh get it k lol
Win
Wain
how do i add values to dicniory?
dictinary
 
7:45 PM
IIRC myDict[key] = value
 
dosent it work like array? append or soemthing?
 
array is like myArray[index] = value
only difference in syntax is key is an object like a string, index is an integer
 
so why it dosent work?
i declare it like this
var MemoryImages : [Int : UIImage]?
and than in the viewdidload
MemoryImages![400] = UIImage(named: "mulia.png")
and it crashes
 
why Int : UIImage ?
your ids and image names are strings, no ?
 
in the db i store the id as ints
and we want to store inside the dicintoary the image it self, what could it give me to save the path?
thats what im doing now
 
7:53 PM
and if you get a crash you need to say what the exception message is and on what line
yes, you store the image as the value
 
and the id as int
correct?
 
if it is an Int, what is the exception ?
 
what you mean?
 
you said it crashes
what is the crash message and which line?
 
oh
thats its nil
lol
k fixed the crash
now ill add it in the for loop
so i add it after he adds it to the db right?
and another question
this dictionary
is the tableview datasource?
or it just for the images?
to handle them i mean
 
8:00 PM
yes
just for images
 
k so i stored all the images inside
what now
in the tableview method>
 
yes
 
what should i do?
 
check if the image is in the dictionary and load it to the image view, if it isn't load it from disk then add to the dictionary and the image view
 
if it dosent on the dictionry for some reason
first add it again to the dictionary and than show it
or straright from the disk?
 
8:06 PM
load from disk, add to dictionary, add to image view
 
alright
and what is the most efficeint way to find value from dictiorny?
 
UIImage? image = myDict[key]
there is no more or less efficient way, just the way
 
hmm
what is the diffrence between reload table and update table?
 
what is update table ?
 
oh lol
nvm
so i did that you said
and it dosent load the images haha
man
brilliant
works very very smootly
!!
 
8:19 PM
good news
 
yes
:D
wait ill run it again
well thats look great
but
i removed all this part
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
// var myPathList : NSArray = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
//
// var myPath = myPathList[0] as String
// myPath = myPath.stringByAppendingPathComponent("\(photoDetails.name).png")
// var image : UIImage = UIImage(contentsOfFile: myPath)!
// println("stucky+\(indexPath.row)")
// dispatch_async(dispatch_get_main_queue()) {
is it needed?
 
you should be loading the images at some point in time - are you doing that on the main thread now ?
it's still best to do it in the background
 
explain please :D
 
if you do it only in the background at the end of the data loading then the table will always have the images and the code you just put isn't needed
 
hm
for better user exprience
i thought about like displaying 20 images each time
when it scroll to the bottom request 20 more
 
8:23 PM
sure
 
but the question is
should i wait that he get the json for the next 20 images, and than run the loop like im doing now
or make 2 opertaions
one for loading the "cells"
second for each cell download the data
so i can make progress bar on the images maybe?
 
well, the text load can be fast and the image slow so separating can help
I'd get the app working and then look at optimisations like that
 
lol this app going to be a huge one
:D
 
good luck with it :)
bye for now :)
 
no noo
dont goooo
:((((
One last thing
when the img dosent on the local storage
i download it
after i download it
in what point should i store it in the dicnitory? after i added it to the db
 

last day (15 days later) »