ios - Swift clearing JSON cache? -
i'm working api data gets updated frequently.
i discovered data not update on phone, when it's updated on server.
after hours on hours trying troubleshoot this, tried deleting app phone, , reinstalling in. , worked.
after further testing discovered it's printing out old json.
once delete app , reinstall it, prints out correct updated json.
from gathered it's issue phone caching old json data somehow.
so how can go clearing cache in swift? or forcing make new request.
(i'm using swiftyjson, although don't think has specific problem)
i did find 1 other question this, it's old (2014 in obj-c) , there no answers.
here's how data:
var request = nsurlrequest(url: formulaapi!) var data = nsurlconnection.sendsynchronousrequest(request, returningresponse: nil, error: nil) var formula = json(data: data!) // loop through api data. (index: string, portfolio: json) in formula["portfolio"] { // save data temporary variables tempstockname = portfolio["name"].stringvalue tempticker = portfolio["ticker"].stringvalue temppurchaseprice = portfolio["purchase_price"].floatvalue.roundto(2) tempweight = portfolio["percentage_weight"].floatvalue latestapiprice = portfolio["latest_price"].floatvalue.roundto(2) tempdaysheld = portfolio["days_owned"].intvalue // continues on quite while, data in above segment getting filled old json data, issue arising before point }
i tried changing request following:
var request = init(formulaapi: nsurl, cachepolicy: nsurlrequestcachepolicy, timeoutinterval: nstimeinterval)
but causes error: "use of local variable 'request' before it's declaration"
any figuring out appreciated!
instead of creating request with,
nsurlrequest(url: formulaapi!)
you should use following method can explicitly set cache policy.
var request = nsurlrequest(url: formulaapi!, cachepolicy: .reloadignoringlocalcachedata, timeoutinterval: 30)
nsurlrequest(url:)
uses default came policy, nsurlrequestuseprotocolcachepolicy
, , time out interval of 60 seconds.
Comments
Post a Comment