evermeer / EVURLCache

a NSURLCache subclass for handling all web requests that use NSURLRequest
Other
297 stars 49 forks source link

Cache not found, wrong path #26

Closed pc11000 closed 8 years ago

pc11000 commented 8 years ago

the file is cached here:

/var/mobile/Containers/Data/Application/50B1343F-AE81-41BE-A7DB-CA21A059481A/Documents/Cache/192.168.1.224/polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js

but then it is loaded from here.. and of course not found

/var/containers/Bundle/Application/78A9AE26-66AA-4264-91AF-CA96DCDBD9EE/TopEase%20Mobile%20App.app/PreCache/192.168.1.224/polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js why its looking in the wrong path?

evermeer commented 8 years ago

The cache will look at both locations. First it will look at the cache folder and if it's not there, it will look in the precache folder. If it's also not there,then it will log the message that the file is not in the precache folder. Maybe it would be better if it also logged that it was not in the cache folder.

Here you can see that both locations are checked: https://github.com/evermeer/EVURLCache/blob/master/EVURLCache/Pod/EVURLCache.swift#L85

But then still... You say it's written to the cache folder. Then it should also have been found there. Maybe you could also log the first path right below line 86. You could duplicate line 88 to there. Do you see the same path as where it's written to?

pc11000 commented 8 years ago

Thx for answering.

I checked it again and tried to log the cache output below line 86.

Here the file has been written in the Cache Writing data to /var/mobile/Containers/Data/Application/87A4143A-1A9F-40B2-B4F6-EC4BC93E4A9F/Documents/Cache/192.168.2.122/polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js

Here it is saved CACHE save file to Cache : /var/mobile/Containers/Data/Application/87A4143A-1A9F-40B2-B4F6-EC4BC93E4A9F/Documents/Cache/192.168.2.122/polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js

then its not found in the precach

CACHE not found /var/containers/Bundle/Application/CEC57FE9-C992-457F-9E98-9FDA64B952AC/TopEase%20Mobile%20App.app/PreCache/192.168.2.122/bower_components/webcomponentsjs/webcomponents-lite.min.js

strange for me, why is it searched in the PreCache? there is no log message that it isnt found in the cache..

evermeer commented 8 years ago

The PreCache is a folder that you can distribute with your app so that all data is available when the app is downloaded. This was the main reason to create EVURLCache. You will be sure the app has the data it needs and you will also have a mechanism to distribute changes or new informations. You can see that there is a PreCache folder in the demo app.

When you change that part of the code to:

        // Is the file in the cache? If not, is the file in the PreCache?
        var storagePath: String = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._cacheDirectory)
        if !NSFileManager.defaultManager().fileExistsAtPath(storagePath) {
            EVURLCache.debugLog("CACHE not found \(storagePath)")
            storagePath  = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._preCacheDirectory)
            if !NSFileManager.defaultManager().fileExistsAtPath(storagePath) {
                EVURLCache.debugLog("PRECACHE not found \(storagePath)")
                return nil
            }
        }

Then you will see 2 log messages CACHE not found .... PRECACHE not found ... The path mentioned behind 'CACHE not found' should be the same as the path mentioned behind 'Writing data to'

pc11000 commented 8 years ago

ok. first I made the PreCache Folder directly in the osx filesystem. Now i made it directly in x-code. Now with your code there is still something wrong

Writing data to /var/mobile/Containers/Data/Application/02E5529B-22BA-497B-9BC0-E60731C10D6C/Documents/Cache/192.168.2.122/polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js

CACHE save file to Cache : /var/mobile/Containers/Data/Application/02E5529B-22BA-497B-9BC0-E60731C10D6C/Documents/Cache/192.168.2.122/polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js

Cache not found /var/mobile/Containers/Data/Application/02E5529B-22BA-497B-9BC0-E60731C10D6C/Documents/Cache/192.168.2.122/bower_components/webcomponentsjs/webcomponents-lite.min.js

PRECACHE not found /var/containers/Bundle/Application/DB6B3998-5DD4-40AC-9683-B44287271A0D/TopEase%20Mobile%20App.app/PreCache/192.168.2.122/bower_components/webcomponentsjs/webcomponents-lite.min.js

the writing/saving and the searching path is not the same.. but why?

evermeer commented 8 years ago

On line 144 you see: let storagePath: String = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._cacheDirectory) This is the path that is used to 'Writing data to'

On line 84 you see: var storagePath: String = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._cacheDirectory) This is the path where the cache will be read from.

In you log output you see that polymer is part of the part when you write and not in the part where you read. That should only happen when there is a difference in the request.

When looking at the code storagePathForRequest, I think this could only happen if there is a difference in the request.URL?.relativePath. You can see this is used on line 217. Could you try adding the following line to the function storagePathForRequest? NSLog("relativePath is \(request.URL?.relativePath)") Do you see the polymer path component appearing and disappearing in that log?

If so, then the requests are nog the same and it's a valid point that it is reported that it can not read that data. The question is then why these requests are different.

pc11000 commented 8 years ago

ok, the relative path is changing quite a lot..

2016-06-23 15:23:33.908 TopEase Mobile App[1861:871463] relativePath is Optional("/polymer/demo")
06/23/2016 15:23:33:931 TopEase Mobile App)[1861:.] EVURLCache.swift(105) cachedResponseForRequest:
    Returning cached data from /var/mobile/Containers/Data/Application/D6768EE2-E84B-4734-B6E3-C3236CBB3135/Documents/Cache/192.168.2.122/polymer/demo/index.html
2016-06-23 15:23:33.945 TopEase Mobile App[1861:871463] relativePath is Optional("/bower_components/webcomponentsjs/webcomponents-lite.min.js")
06/23/2016 15:23:33:947 TopEase Mobile App)[1861:.] EVURLCache.swift(86) cachedResponseForRequest:
    Cache not found /var/mobile/Containers/Data/Application/D6768EE2-E84B-4734-B6E3-C3236CBB3135/Documents/Cache/192.168.2.122/bower_components/webcomponentsjs/webcomponents-lite.min.js
2016-06-23 15:23:33.953 TopEase Mobile App[1861:871463] relativePath is Optional("/bower_components/webcomponentsjs/webcomponents-lite.min.js")
06/23/2016 15:23:33:954 TopEase Mobile App)[1861:.] EVURLCache.swift(89) cachedResponseForRequest:
    PRECACHE not found /var/containers/Bundle/Application/C54EF7C5-9514-4B22-B37E-6637F099BCF6/TopEase%20Mobile%20App.app/PreCache/192.168.2.122/bower_components/webcomponentsjs/webcomponents-lite.min.js

I added the bower_components folder directly in to the folder before in the PreCache

/192.168.2.122/bower_components/

instead of

/192.168.2.122/polymer/bower_components/

but it still not finding the file

evermeer commented 8 years ago

Is the PreCache folder included in the build? (select the folder in the project navigator, and see the 'Target Membership' in the right pane) You can also check that by using the finder and going to the folder /var/containers/Bundle/Application/C54EF7C5-9514-4B22-B37E-6637F099BCF6/TopEase%20Mobile%20App.app/PreCache/ You should see the files.

You could also see what happens in the folder /var/mobile/Containers/Data/Application/D6768EE2-E84B-4734-B6E3-C3236CBB3135/Documents/Cache

pc11000 commented 8 years ago

I dont find this path in the finder but the target membership is set.

where is this /var/...

evermeer commented 8 years ago

I think you are running on a device. Then you can only find it with a tool like iPhone explorer. If you run it in a simulator you will be able to access the paths that are in the log.

pc11000 commented 8 years ago

Yes I am testing on a device. Now I copied all the files from the polymer folder one folder up (in iPhone explorer tool called iMazing) and now its working.

So all the files are there stored. Just in the wrong folder or he is searching in the wrong folder

What I dont see there is the PreCache folder.. should I see it in the iPhone explorer?

evermeer commented 8 years ago

Good news that you have found how to fix this.

Remember that you should also fix that in your PreCache folder inside your project. To be sure it works, remove the app from your device and run it again.

Then the question remainig is why you did expect polimer to be in the path. If you open your index.html in safari on your computer and inspect the network traffic, do you see polimer as part of the path when it request the .js file? I'm almost positive that it's also not there.

pc11000 commented 8 years ago

hmm i checked it and polymer is a part of the path there:

Request URL:http://192.168.1.18:8080/polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js

its just not in path when loading from the cache.. is it possible to manually but the polymer folder in the loading path?

evermeer commented 8 years ago

I am using request.URL?.relativePath Maybe that's wrong and i should use request.URL?.path

xcdoc://?url=developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/index.html#//apple_ref/occ/instp/NSURL/path

I will do some tests. I get the feeling that this might solve your issue.

evermeer commented 8 years ago

OK, I just tested it. When having an html file at path http://server.com/rootmap/index.html and using relative paths in your html like "../map/file.js" Then the relativePath will indeed only return map/file.js The path will return rootmap/map/file.js

I have fixed this and pushed this as version 2.8.0 to GitHub and Cocoapods.

Thanks for reporting this and your effort to make the problem clear to me.

pc11000 commented 8 years ago

what did you changed ? just this? :

if let path = request.URL?.path

for me its still not working..

pc11000 commented 8 years ago

I'm still not sure why the writing and loading path isnt the same...

But I could solve the issue by removing the one folder (polymer) which caused problems in the path..

let newPath = path.stringByReplacingOccurrencesOfString("/polymer", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                localUrl = "\(host)\(newPath)"
evermeer commented 8 years ago

just let me know if you still have a problem with this. For now I will close this because I'm not able to reproduce the issue. If you still have the issue, could you point me out how I could reproduce it?