Sync.swift 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CoreData
  22. import UIKit
  23. /**
  24. Class to handle server sync.
  25. */
  26. class Sync{
  27. var initial: Bool = false
  28. /**
  29. Starts the sync process.
  30. Always asynchronously.
  31. */
  32. init(){
  33. let url = buildUrl();
  34. sync(url: url)
  35. }
  36. /**
  37. Starts the sync process, synchronously or asynchronously.
  38. :param: synchronous True for synchronous sync, false for asynchronously
  39. */
  40. init(synchronous: Bool){
  41. if synchronous == true{
  42. NSLog(":SYNC:LOG: Synchronous sync started.")
  43. self.initial = true
  44. }
  45. let url = buildUrl();
  46. sync(url: url)
  47. }
  48. /**
  49. Builds the URL to perform the sync against.
  50. :returns: The URL.
  51. */
  52. func buildUrl() -> URL{
  53. let url = URL(string: "https://margolariak.com/API/v1/sync.php?client=com.margolariak.app")
  54. //TODO add parameters
  55. return url!
  56. }
  57. /**
  58. Performs an asynchronous sync.
  59. It fethes the info from the server and stores as Core Data
  60. :param: url The url to sync.
  61. */
  62. func sync(url: URL){
  63. NSLog(":SYNC:LOG: Sync started.")
  64. //Synchronously get data
  65. let task = URLSession.shared.dataTask(with: url) { data, response, error in
  66. guard error == nil else {
  67. NSLog(":SYNC:ERROR: Unknown error.")
  68. return
  69. }
  70. guard let data = data else {
  71. NSLog(":SYNC:ERROR: Data is empty.")
  72. return
  73. }
  74. NSLog(":SYNC:LOG: Data received.")
  75. var strData = String(data:data, encoding: String.Encoding.utf8)
  76. let dataIdx = strData?.indexOf(target: "{\"data\"")
  77. strData = strData!.subStr(start: dataIdx!, end: strData!.length - 1)
  78. //TODO: do something with versions
  79. //Get tables
  80. let dataActivity : [String] = self.getTable(data: strData!, table: "activity")
  81. let dataActivityComment : [String] = self.getTable(data: strData!, table: "activity_comment")
  82. let dataActivityImage : [String] = self.getTable(data: strData!, table: "activity_image")
  83. let dataActivityTag : [String] = self.getTable(data: strData!, table: "activity_tag")
  84. let dataActivityItinerary : [String] = self.getTable(data: strData!, table: "activity_itinerary")
  85. let dataAlbum : [String] = self.getTable(data: strData!, table: "album")
  86. let dataFestival : [String] = self.getTable(data: strData!, table: "festival")
  87. let dataFestivalDay : [String] = self.getTable(data: strData!, table: "festival_day")
  88. let dataFestivalEvent : [String] = self.getTable(data: strData!, table: "festival_event")
  89. //let dataFestivalEventImage : [String] = self.getTable(data: strData!, table: "festival_event_image")
  90. let dataFestivalOffer : [String] = self.getTable(data: strData!, table: "festival_offer")
  91. let dataPeople : [String] = self.getTable(data: strData!, table: "people")
  92. let dataPhoto : [String] = self.getTable(data: strData!, table: "photo")
  93. let dataPhotoAlbum : [String] = self.getTable(data: strData!, table: "photo_album")
  94. //let dataPhotoComment : [String] = self.getTable(data: strData!, table: "photo_comment")
  95. let dataPlace : [String] = self.getTable(data: strData!, table: "place")
  96. let dataPost : [String] = self.getTable(data: strData!, table: "post")
  97. let dataPostComment : [String] = self.getTable(data: strData!, table: "post_comment")
  98. let dataPostImage : [String] = self.getTable(data: strData!, table: "post_image")
  99. let dataPostTag : [String] = self.getTable(data: strData!, table: "post_tag")
  100. let dataSponsor : [String] = self.getTable(data: strData!, table: "sponsor")
  101. let dataSettings: [String] = self.getTable(data: strData!, table: "settings")
  102. //One by one, save the received data
  103. self.saveTableActivity(entries : dataActivity)
  104. self.saveTableActivityComment(entries : dataActivityComment)
  105. self.saveTableActivityImage(entries : dataActivityImage)
  106. self.saveTableActivityTag(entries : dataActivityTag)
  107. self.saveTableActivityItinerary(entries : dataActivityItinerary)
  108. self.saveTablePost(entries: dataPost)
  109. self.saveTablePostComment(entries: dataPostComment)
  110. self.saveTablePostImage(entries: dataPostImage)
  111. self.saveTablePostTag(entries: dataPostTag)
  112. self.saveTablePlace(entries: dataPlace)
  113. self.saveTablePeople(entries: dataPeople)
  114. self.saveTableAlbum(entries: dataAlbum)
  115. self.saveTablePhoto(entries: dataPhoto)
  116. self.saveTablePhotoAlbum(entries: dataPhotoAlbum)
  117. self.saveTableFestival(entries: dataFestival)
  118. self.saveTableFestivalDay(entries: dataFestivalDay)
  119. self.saveTableFestivalOffer(entries: dataFestivalOffer)
  120. self.saveTableFestivalEvent(entries: dataFestivalEvent)
  121. self.saveTableSponsor(entries: dataSponsor)
  122. self.saveSettings(entries: dataSettings)
  123. //DEBUG: Tester, Debug only
  124. // Test if a entity has entries
  125. /*let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  126. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  127. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  128. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest() //Entity to debug
  129. do {
  130. //go get the results
  131. let searchResults = try context.fetch(fetchRequest)
  132. NSLog(":SYNC:DEBUG: Num of results = \(searchResults.count).")
  133. for r in searchResults as [NSManagedObject] {
  134. NSLog(":SYNC:DEBUG: id: \(r.value(forKey: "id")).")
  135. }
  136. } catch {
  137. NSLog(":SYNC:DEBUG: Error with request: \(error).")
  138. }*/
  139. //End tester
  140. // If it's the initial sync, hide the segue
  141. if self.initial == true{
  142. NSLog(":SYNC:LOG: Finishing synchronous sync.")
  143. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  144. delegate.controller?.initialSync(showScreen: false)
  145. }
  146. }
  147. task.resume()
  148. }
  149. /**
  150. Extracts table rows from the raw data received.
  151. :param: param Received data in JSON format.
  152. :param: table The name of the table to look for.
  153. :return: Array of strings containing the rows of the table, in JSON format.
  154. */
  155. func getTable(data: String, table: String) -> [String]{
  156. //Does the table exists?
  157. if data.indexOf(target: "\"\(table)\":[") == nil{
  158. NSLog(":SYNC:LOG: Empty table: \(table).")
  159. return [String]()
  160. }
  161. let iS : Int? = data.indexOf(target: "\"\(table)\":[")
  162. var str = data.subStr(start: iS!, end: data.length - 1)
  163. let iE : Int? = str.indexOf(target: "}]}")!
  164. str = str.subStr(start: 0, end: iE!)
  165. str = str.subStr(start: table.length + 3,end: str.length - 1)
  166. //Separate the entries
  167. var entries : [String] = str.components(separatedBy: "},{")
  168. //Modify the first and the last one to remove bracers
  169. entries[0] = entries[0].subStr(start: 1, end: entries[0].length - 1)
  170. entries[entries.count - 1] = entries[entries.count - 1].subStr(start: 0, end: entries[entries.count - 1].length - 2)
  171. NSLog(":SYNC:LOG: Table \(table) has \(entries.count) rows.")
  172. //Return the array
  173. return entries
  174. }
  175. /**
  176. Saves the data in the table.
  177. :param: entries Array of strings containing the rows of the table, in JSON format.
  178. */
  179. func saveTablePlace(entries : [String]){
  180. NSLog(":SYNC:LOG: Saving table Place.")
  181. //Set up context
  182. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  183. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  184. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  185. let entity = NSEntityDescription.entity(forEntityName: "Place", in: context)
  186. var row: NSManagedObject
  187. //Delete all previous entries
  188. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Place")
  189. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  190. do {
  191. try context.execute(request)
  192. } catch let error as NSError {
  193. NSLog(":SYNC:ERROR: Could not clean up Places entity: \(error), \(error.userInfo).")
  194. } catch {
  195. NSLog(":SYNC:ERROR: Could not clean up Places entity.")
  196. }
  197. //Loop new entries
  198. for entry in entries{
  199. //Get id
  200. var str = entry
  201. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  202. //Get name_es
  203. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  204. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  205. //Get name_en
  206. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  207. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  208. //Get name_eu
  209. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  210. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  211. //Get address_es
  212. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  213. let address_es : String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  214. //Get address_en
  215. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  216. let address_en : String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  217. //Get address_eu
  218. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  219. let address_eu : String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  220. //Get cp
  221. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  222. let cp : String = str.subStr(start : str.indexOf(target : "\"cp\":")! + 6, end : str.indexOf(target : ",\"")! - 2)
  223. //Get lat
  224. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  225. let lat : Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  226. //Get lon
  227. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  228. let lon : Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  229. //Save CoreData
  230. row = NSManagedObject(entity: entity!, insertInto: context)
  231. row.setValue(id, forKey: "id")
  232. row.setValue(name_es, forKey: "name_es")
  233. row.setValue(name_en, forKey: "name_en")
  234. row.setValue(name_eu, forKey: "name_eu")
  235. row.setValue(address_es, forKey: "address_es")
  236. row.setValue(address_en, forKey: "address_en")
  237. row.setValue(address_eu, forKey: "address_eu")
  238. row.setValue(cp, forKey: "cp")
  239. row.setValue(lat, forKey: "lat")
  240. row.setValue(lon, forKey: "lon")
  241. do {
  242. try context.save()
  243. } catch let error as NSError {
  244. NSLog(":SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  245. } catch {
  246. NSLog(":SYNC:ERROR: Could not store Place with id \(id).")
  247. }
  248. }
  249. }
  250. /**
  251. Saves the data in the table.
  252. :param: entries Array of strings containing the rows of the table, in JSON format.
  253. */
  254. func saveTablePeople(entries : [String]){
  255. NSLog(":SYNC:LOG: Saving table People.")
  256. //Set up context
  257. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  258. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  259. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  260. let entity = NSEntityDescription.entity(forEntityName: "People", in: context)
  261. var row: NSManagedObject
  262. //Delete all previous entries
  263. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "People")
  264. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  265. do {
  266. try context.execute(request)
  267. } catch let error as NSError {
  268. NSLog(":SYNC:ERROR: Could not clean up People entity: \(error), \(error.userInfo).")
  269. } catch {
  270. NSLog(":SYNC:ERROR: Could not clean up People entity.")
  271. }
  272. //Loop new entries
  273. for entry in entries{
  274. row = NSManagedObject(entity: entity!, insertInto: context)
  275. //Get id
  276. var str = entry
  277. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  278. row.setValue(id, forKey: "id")
  279. //Get name_es
  280. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  281. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  282. row.setValue(name_es, forKey: "name_es")
  283. //Get name_en
  284. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  285. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  286. row.setValue(name_en, forKey: "name_en")
  287. //Get name_eu
  288. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  289. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  290. row.setValue(name_eu, forKey: "name_eu")
  291. //Get link
  292. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  293. let link : String = str.subStr(start : str.indexOf(target : "\"link\":")! + 8, end : str.length - 2)
  294. row.setValue(link, forKey: "link")
  295. //Save CoreData
  296. do {
  297. try context.save()
  298. } catch let error as NSError {
  299. NSLog(":SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  300. } catch {
  301. NSLog(":SYNC:ERROR: Could not store Place with id \(id).")
  302. }
  303. }
  304. }
  305. /**
  306. Saves the data in the table.
  307. :param: entries Array of strings containing the rows of the table, in JSON format.
  308. */
  309. func saveTablePost(entries : [String]){
  310. NSLog(":SYNC:LOG: Saving table Post.")
  311. let dateFormatter = DateFormatter()
  312. dateFormatter.timeZone = TimeZone.ReferenceType.local
  313. //Set up context
  314. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  315. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  316. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  317. let entity = NSEntityDescription.entity(forEntityName: "Post", in: context)
  318. var row: NSManagedObject
  319. //Delete all previous entries
  320. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post")
  321. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  322. do {
  323. try context.execute(request)
  324. } catch let error as NSError {
  325. NSLog(":SYNC:ERROR: Could not clean up Post entity: \(error), \(error.userInfo).")
  326. } catch {
  327. NSLog(":SYNC:ERROR: Could not clean up Post entity.")
  328. }
  329. //Loop new entries
  330. for entry in entries{
  331. //Get id
  332. var str = entry
  333. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  334. //Get permalink
  335. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  336. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  337. //Get title_es
  338. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  339. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  340. //Get title_en
  341. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  342. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  343. //Get title_eu
  344. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  345. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  346. //Get text_es
  347. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  348. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  349. //Get text_en
  350. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  351. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  352. //Get text_eu
  353. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  354. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  355. //Get comments
  356. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  357. let comments : Int = Int(str.subStr(start : str.indexOf(target : "\"comments\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  358. //Get username
  359. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  360. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  361. //Get dtime
  362. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  363. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  364. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.length - 2))!
  365. //Save CoreData
  366. row = NSManagedObject(entity: entity!, insertInto: context)
  367. //set the entity values
  368. row.setValue(id, forKey: "id")
  369. row.setValue(permalink, forKey: "permalink")
  370. row.setValue(dtime, forKey: "dtime")
  371. row.setValue(username, forKey: "username")
  372. row.setValue(comments, forKey: "comments")
  373. row.setValue(title_es, forKey: "title_es")
  374. row.setValue(title_en, forKey: "title_en")
  375. row.setValue(title_eu, forKey: "title_eu")
  376. row.setValue(text_es, forKey: "text_es")
  377. row.setValue(text_en, forKey: "text_en")
  378. row.setValue(text_eu, forKey: "text_eu")
  379. do {
  380. try context.save()
  381. } catch let error as NSError {
  382. NSLog(":SYNC:ERROR: Could not store Post with id \(id): \(error), \(error.userInfo).")
  383. } catch {
  384. NSLog(":SYNC:ERROR: Could not store Post with id \(id).")
  385. }
  386. }
  387. }
  388. /**
  389. Saves the data in the table.
  390. :param: entries Array of strings containing the rows of the table, in JSON format.
  391. */
  392. func saveTablePostComment(entries : [String]){
  393. NSLog(":SYNC:LOG: Saving table Post_comment.")
  394. let dateFormatter = DateFormatter()
  395. dateFormatter.timeZone = TimeZone.ReferenceType.local
  396. //Set up context
  397. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  398. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  399. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  400. let entity = NSEntityDescription.entity(forEntityName: "Post_comment", in: context)
  401. var row: NSManagedObject
  402. //Delete all previous entries
  403. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_comment")
  404. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  405. do {
  406. try context.execute(request)
  407. } catch let error as NSError {
  408. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  409. } catch {
  410. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity.")
  411. }
  412. //Loop new entries
  413. for entry in entries{
  414. //Get id
  415. var str = entry
  416. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  417. //Get activity
  418. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  419. let post : String = str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  420. //Get text
  421. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  422. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  423. //Get dtime
  424. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  425. dateFormatter.dateFormat = "yyyy-MM-dd"
  426. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  427. //Get username
  428. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  429. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  430. //Get lang
  431. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  432. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  433. //Save CoreData
  434. row = NSManagedObject(entity: entity!, insertInto: context)
  435. row.setValue(id, forKey: "id")
  436. row.setValue(post, forKey: "post")
  437. row.setValue(text, forKey: "text")
  438. row.setValue(dtime, forKey: "dtime")
  439. row.setValue(username, forKey: "username")
  440. row.setValue(lang, forKey: "lang")
  441. do {
  442. try context.save()
  443. } catch let error as NSError {
  444. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  445. } catch {
  446. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id).")
  447. }
  448. }
  449. }
  450. /**
  451. Saves the data in the table.
  452. :param: entries Array of strings containing the rows of the table, in JSON format.
  453. */
  454. func saveTablePostImage(entries : [String]){
  455. NSLog(":SYNC:LOG: Saving table Post_image.")
  456. //Set up context
  457. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  458. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  459. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  460. let entity = NSEntityDescription.entity(forEntityName: "Post_image", in: context)
  461. var row: NSManagedObject
  462. //Delete all previous entries
  463. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_image")
  464. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  465. do {
  466. try context.execute(request)
  467. } catch let error as NSError {
  468. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  469. } catch {
  470. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity.")
  471. }
  472. //Loop new entries
  473. for entry in entries{
  474. //Get id
  475. var str = entry
  476. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  477. //Get activity
  478. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  479. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  480. //Get image
  481. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  482. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  483. //Get idx
  484. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  485. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  486. //Save CoreData
  487. row = NSManagedObject(entity: entity!, insertInto: context)
  488. row.setValue(id, forKey: "id")
  489. row.setValue(post, forKey: "post")
  490. row.setValue(image, forKey: "image")
  491. row.setValue(idx, forKey: "idx")
  492. do {
  493. try context.save()
  494. } catch let error as NSError {
  495. NSLog(":SYNC:ERROR: Could not store Post_image with id \(id): \(error), \(error.userInfo).")
  496. } catch {
  497. NSLog(":SYNC:ERROR: Could not store Post_image with id \(id).")
  498. }
  499. }
  500. }
  501. /**
  502. Saves the data in the table.
  503. :param: entries Array of strings containing the rows of the table, in JSON format.
  504. */
  505. func saveTablePostTag(entries : [String]){
  506. NSLog(":SYNC:LOG: Saving table Post_tag.")
  507. //Set up context
  508. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  509. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  510. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  511. let entity = NSEntityDescription.entity(forEntityName: "Post_tag", in: context)
  512. var row: NSManagedObject
  513. //Delete all previous entries
  514. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_tag")
  515. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  516. do {
  517. try context.execute(request)
  518. } catch let error as NSError {
  519. NSLog(":SYNC:ERROR: Could not clean up Post_tag entity: \(error), \(error.userInfo).")
  520. } catch {
  521. NSLog(":SYNC:ERROR: Could not clean up Post_tag entity.")
  522. }
  523. //Loop new entries
  524. for entry in entries{
  525. //Get activity
  526. var str = entry
  527. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  528. //Get tag
  529. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  530. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  531. //Save CoreData
  532. row = NSManagedObject(entity: entity!, insertInto: context)
  533. row.setValue(post, forKey: "post")
  534. row.setValue(tag, forKey: "tag")
  535. do {
  536. try context.save()
  537. } catch let error as NSError {
  538. NSLog(":SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag): \(error), \(error.userInfo).")
  539. } catch {
  540. NSLog(":SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag).")
  541. }
  542. }
  543. }
  544. /**
  545. Saves the data in the table.
  546. :param: entries Array of strings containing the rows of the table, in JSON format.
  547. */
  548. func saveTableActivity(entries : [String]){
  549. NSLog(":SYNC:LOG: Saving table Activity.")
  550. let dateFormatter = DateFormatter()
  551. dateFormatter.timeZone = TimeZone.ReferenceType.local
  552. //Set up context
  553. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  554. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  555. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  556. let entity = NSEntityDescription.entity(forEntityName: "Activity", in: context)
  557. var row: NSManagedObject
  558. //Delete all previous entries
  559. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity")
  560. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  561. do {
  562. try context.execute(request)
  563. } catch let error as NSError {
  564. NSLog(":SYNC:ERROR: Could not clean up Activity entity: \(error), \(error.userInfo).")
  565. } catch {
  566. NSLog(":SYNC:ERROR: Could not clean up Activity entity.")
  567. }
  568. //Loop new entries
  569. for entry in entries{
  570. //Get id
  571. var str = entry
  572. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  573. //Get permalink
  574. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  575. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  576. //Get date
  577. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  578. dateFormatter.dateFormat = "yyyy-MM-dd"
  579. let date = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  580. //Get city
  581. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  582. let city : String = str.subStr(start : str.indexOf(target : "\"city\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  583. //Get title_es
  584. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  585. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  586. //Get title_en
  587. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  588. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  589. //Get title_eu
  590. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  591. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  592. //Get text_es
  593. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  594. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  595. //Get text_eu
  596. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  597. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  598. //Get text_en
  599. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  600. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  601. //Get after_es
  602. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  603. var after_es : String = str.subStr(start : str.indexOf(target : "\"after_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  604. if (after_es == "ul"){ //From "null"
  605. after_es = ""
  606. }
  607. //Get after_en
  608. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  609. var after_en : String = str.subStr(start : str.indexOf(target : "\"after_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  610. if (after_en == "ul"){ //From "null"
  611. after_en = ""
  612. }
  613. //Get after_eu
  614. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  615. var after_eu : String = str.subStr(start : str.indexOf(target : "\"after_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  616. if (after_eu == "ul"){ //From "null"
  617. after_eu = ""
  618. }
  619. //Get price
  620. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  621. let price : Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  622. //Get inscription
  623. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  624. let inscription : Int = Int(str.subStr(start : str.indexOf(target : "\"inscription\":")! + 15, end : str.indexOf(target : ",\"")! - 2))!
  625. //Get max_people
  626. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  627. let maxPeoplePre : String = str.subStr(start : str.indexOf(target : "\"max_people\":")! + 14, end : str.indexOf(target : ",")! - 2)
  628. var maxPeople = -1
  629. if (maxPeoplePre != "ul"){ //From "null"
  630. maxPeople = Int(maxPeoplePre)!
  631. }
  632. //Get album
  633. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  634. let albumPre = str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2)
  635. var album = -1
  636. if (albumPre != "ul"){ //From "null"
  637. album = Int(albumPre)!
  638. }
  639. //Skipping dtime
  640. //Skipping comments
  641. //Save CoreData
  642. row = NSManagedObject(entity: entity!, insertInto: context)
  643. //set the entity values
  644. row.setValue(id, forKey: "id")
  645. row.setValue(permalink, forKey: "permalink")
  646. row.setValue(date, forKey: "date")
  647. row.setValue(city, forKey: "city")
  648. row.setValue(title_es, forKey: "title_es")
  649. row.setValue(title_en, forKey: "title_en")
  650. row.setValue(title_eu, forKey: "title_eu")
  651. row.setValue(text_es, forKey: "text_es")
  652. row.setValue(text_en, forKey: "text_en")
  653. row.setValue(text_eu, forKey: "text_eu")
  654. row.setValue(after_es, forKey: "after_es")
  655. row.setValue(after_en, forKey: "after_en")
  656. row.setValue(after_eu, forKey: "after_eu")
  657. row.setValue(price, forKey: "price")
  658. row.setValue(inscription, forKey: "inscription")
  659. row.setValue(maxPeople, forKey: "max_people")
  660. if (album != -1){
  661. row.setValue(album, forKey: "album")
  662. }
  663. do {
  664. try context.save()
  665. } catch let error as NSError {
  666. NSLog(":SYNC:ERROR: Could not store Activity with id \(id): \(error), \(error.userInfo).")
  667. } catch {
  668. NSLog(":SYNC:ERROR: Could not store Activity with id \(id).")
  669. }
  670. }
  671. }
  672. /**
  673. Saves the data in the table.
  674. :param: entries Array of strings containing the rows of the table, in JSON format.
  675. */
  676. func saveTableActivityComment(entries : [String]){
  677. NSLog(":SYNC:LOG: Saving table Activity_comment.")
  678. let dateFormatter = DateFormatter()
  679. dateFormatter.timeZone = TimeZone.ReferenceType.local
  680. //Set up context
  681. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  682. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  683. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  684. let entity = NSEntityDescription.entity(forEntityName: "Activity_comment", in: context)
  685. var row: NSManagedObject
  686. //Delete all previous entries
  687. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_comment")
  688. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  689. do {
  690. try context.execute(request)
  691. } catch let error as NSError {
  692. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  693. } catch {
  694. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity.")
  695. }
  696. //Loop new entries
  697. for entry in entries{
  698. //Get id
  699. var str = entry
  700. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  701. //Get activity
  702. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  703. let activity : String = str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  704. //Get text
  705. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  706. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  707. //Get dtime
  708. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  709. dateFormatter.dateFormat = "yyyy-MM-dd"
  710. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  711. //Get username
  712. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  713. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  714. //Get lang
  715. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  716. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  717. //Save CoreData
  718. row = NSManagedObject(entity: entity!, insertInto: context)
  719. row.setValue(id, forKey: "id")
  720. row.setValue(activity, forKey: "activity")
  721. row.setValue(text, forKey: "text")
  722. row.setValue(dtime, forKey: "dtime")
  723. row.setValue(username, forKey: "username")
  724. row.setValue(lang, forKey: "lang")
  725. do {
  726. try context.save()
  727. } catch let error as NSError {
  728. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  729. } catch {
  730. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id).")
  731. }
  732. }
  733. }
  734. /**
  735. Saves the data in the table.
  736. :param: entries Array of strings containing the rows of the table, in JSON format.
  737. */
  738. func saveTableActivityImage(entries : [String]){
  739. NSLog(":SYNC:LOG: Saving table Activity_image.")
  740. //Set up context
  741. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  742. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  743. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  744. let entity = NSEntityDescription.entity(forEntityName: "Activity_image", in: context)
  745. var row: NSManagedObject
  746. //Delete all previous entries
  747. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_image")
  748. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  749. do {
  750. try context.execute(request)
  751. } catch let error as NSError {
  752. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  753. } catch {
  754. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity.")
  755. }
  756. //Loop new entries
  757. for entry in entries{
  758. //Get id
  759. var str = entry
  760. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  761. //Get activity
  762. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  763. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  764. //Get image
  765. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  766. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  767. //Get idx
  768. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  769. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  770. //Save CoreData
  771. row = NSManagedObject(entity: entity!, insertInto: context)
  772. row.setValue(id, forKey: "id")
  773. row.setValue(activity, forKey: "activity")
  774. row.setValue(image, forKey: "image")
  775. row.setValue(idx, forKey: "idx")
  776. do {
  777. try context.save()
  778. } catch let error as NSError {
  779. NSLog(":SYNC:ERROR: Could not store Activity_image with id \(id): \(error), \(error.userInfo).")
  780. } catch {
  781. NSLog(":SYNC:ERROR: Could not store Activity_image with id \(id).")
  782. }
  783. }
  784. }
  785. /**
  786. Saves the data in the table.
  787. :param: entries Array of strings containing the rows of the table, in JSON format.
  788. */
  789. func saveTableActivityTag(entries : [String]){
  790. NSLog(":SYNC:LOG: Saving table Activity_tag.")
  791. //Set up context
  792. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  793. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  794. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  795. let entity = NSEntityDescription.entity(forEntityName: "Activity_tag", in: context)
  796. var row: NSManagedObject
  797. //Delete all previous entries
  798. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_tag")
  799. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  800. do {
  801. try context.execute(request)
  802. } catch let error as NSError {
  803. NSLog(":SYNC:ERROR: Could not clean up Activity_tag entity: \(error), \(error.userInfo).")
  804. } catch {
  805. NSLog(":SYNC:ERROR: Could not clean up Activity_tag entity.")
  806. }
  807. //Loop new entries
  808. for entry in entries{
  809. //Get activity
  810. var str = entry
  811. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  812. //Get tag
  813. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  814. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  815. //Save CoreData
  816. row = NSManagedObject(entity: entity!, insertInto: context)
  817. row.setValue(activity, forKey: "activity")
  818. row.setValue(tag, forKey: "tag")
  819. do {
  820. try context.save()
  821. } catch let error as NSError {
  822. NSLog(":SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag): Error: \(error), \(error.userInfo).")
  823. } catch {
  824. NSLog(":SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag).")
  825. }
  826. }
  827. }
  828. /**
  829. Saves the data in the table.
  830. :param: entries Array of strings containing the rows of the table, in JSON format.
  831. */
  832. //CHECK: New (and probably better format than the others.)
  833. func saveTableActivityItinerary(entries : [String]){
  834. NSLog(":SYNC:LOG: Saving table Activity_itinerary.")
  835. let dateFormatter = DateFormatter()
  836. dateFormatter.timeZone = TimeZone.ReferenceType.local
  837. //Set up context
  838. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  839. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  840. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  841. let entity = NSEntityDescription.entity(forEntityName: "Activity_itinerary", in: context)
  842. var row: NSManagedObject
  843. //Delete all previous entries
  844. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_itinerary")
  845. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  846. do {
  847. try context.execute(request)
  848. } catch let error as NSError {
  849. NSLog(":SYNC:ERROR: Could not clean up Activity_itinerary entity: \(error), \(error.userInfo).")
  850. } catch {
  851. NSLog(":SYNC:ERROR: Could not clean up Activity_itinerary entity.")
  852. }
  853. //Loop new entries
  854. for entry in entries{
  855. row = NSManagedObject(entity: entity!, insertInto: context)
  856. //Get id
  857. var str = entry
  858. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  859. row.setValue(id, forKey: "id")
  860. //Get activity
  861. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  862. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  863. row.setValue(activity, forKey: "activity")
  864. //Get name_es
  865. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  866. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  867. row.setValue(name_es, forKey: "name_es")
  868. //Get name_en
  869. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  870. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  871. row.setValue(name_en, forKey: "name_en")
  872. //Get name_eu
  873. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  874. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  875. row.setValue(name_eu, forKey: "name_eu")
  876. //Get description_es
  877. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  878. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  879. if (description_es != "ul"){ //From "null"
  880. row.setValue(description_es, forKey: "description_es")
  881. }
  882. //Get description_en
  883. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  884. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  885. if (description_en != "ul"){ //From "null"
  886. row.setValue(description_en, forKey: "description_en")
  887. }
  888. //Get description_eu
  889. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  890. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  891. if (description_eu != "ul"){ //From "null"
  892. row.setValue(description_eu, forKey: "description_eu")
  893. }
  894. //Get start
  895. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  896. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  897. let start = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  898. row.setValue(start, forKey: "start")
  899. //Get end
  900. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  901. let end = str.subStr(start : str.indexOf(target : "\"end\":")! + 7, end : str.indexOf(target : ",\"")! - 2)
  902. if (end != "ul"){ //From "null"
  903. row.setValue(dateFormatter.date(from: end), forKey: "end")
  904. }
  905. //Get place
  906. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  907. let place : Int = Int(str.subStr(start : str.indexOf(target : "\"place\":")! + 9, end : str.length - 2))!
  908. row.setValue(place, forKey: "place")
  909. //Save CoreData
  910. do {
  911. try context.save()
  912. } catch let error as NSError {
  913. NSLog(":SYNC:ERROR: Could not store Activity_itinerary with id \(id): \(error), \(error.userInfo).")
  914. } catch {
  915. NSLog(":SYNC:ERROR: Could not store Activity_itinerary with id \(id).")
  916. }
  917. }
  918. }
  919. /**
  920. Saves the data in the table.
  921. :param: entries Array of strings containing the rows of the table, in JSON format.
  922. */
  923. func saveTableAlbum(entries : [String]){
  924. NSLog(":SYNC:LOG: Saving table Album.")
  925. //Set up context
  926. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  927. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  928. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  929. let entity = NSEntityDescription.entity(forEntityName: "Album", in: context)
  930. var row: NSManagedObject
  931. //Delete all previous entries
  932. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Album")
  933. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  934. do {
  935. try context.execute(request)
  936. } catch let error as NSError {
  937. NSLog(":SYNC:ERROR: Could not clean up Album: \(error), \(error.userInfo).")
  938. } catch {
  939. NSLog(":SYNC:ERROR: Could not clean up Album entity.")
  940. }
  941. //Loop new entries
  942. for entry in entries{
  943. row = NSManagedObject(entity: entity!, insertInto: context)
  944. //Get id
  945. var str = entry
  946. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  947. row.setValue(id, forKey: "id")
  948. //Get permalink
  949. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  950. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  951. row.setValue(permalink, forKey: "permalink")
  952. //Get title_es
  953. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  954. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  955. row.setValue(title_es, forKey: "title_es")
  956. //Get title_en
  957. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  958. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  959. row.setValue(title_en, forKey: "title_en")
  960. //Get title_eu
  961. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  962. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  963. row.setValue(title_eu, forKey: "title_eu")
  964. //Get description_es
  965. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  966. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  967. if (description_es != "ul"){ //From "null"
  968. row.setValue(description_es, forKey: "description_es")
  969. }
  970. //Get description_en
  971. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  972. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  973. if (description_en != "ul"){ //From "null"
  974. row.setValue(description_en, forKey: "description_en")
  975. }
  976. //Get description_eu
  977. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  978. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  979. if (description_eu != "ul"){ //From "null"
  980. row.setValue(description_eu, forKey: "description_eu")
  981. }
  982. //Get open
  983. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  984. let open : Int = Int(str.subStr(start : str.indexOf(target : "\"open\":")! + 8, end : str.length - 2))!
  985. row.setValue(open, forKey: "open")
  986. //Save CoreData
  987. do {
  988. try context.save()
  989. } catch let error as NSError {
  990. NSLog(":SYNC:ERROR: Could not store Album with id \(id): \(error), \(error.userInfo).")
  991. } catch {
  992. NSLog(":SYNC:ERROR: Could not store Album with id \(id).")
  993. }
  994. }
  995. }
  996. /**
  997. Saves the data in the table.
  998. :param: entries Array of strings containing the rows of the table, in JSON format.
  999. */
  1000. func saveTablePhoto(entries : [String]){
  1001. NSLog(":SYNC:LOG: Saving table Photo.")
  1002. let dateFormatter = DateFormatter()
  1003. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1004. //Set up context
  1005. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1006. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1007. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1008. let entity = NSEntityDescription.entity(forEntityName: "Photo", in: context)
  1009. var row: NSManagedObject
  1010. //Delete all previous entries
  1011. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Photo")
  1012. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1013. do {
  1014. try context.execute(request)
  1015. } catch let error as NSError {
  1016. NSLog(":SYNC:ERROR: Could not clean up Photo: \(error), \(error.userInfo).")
  1017. } catch {
  1018. NSLog(":SYNC:ERROR: Could not clean up Photo entity.")
  1019. }
  1020. //Loop new entries
  1021. for entry in entries{
  1022. row = NSManagedObject(entity: entity!, insertInto: context)
  1023. //Get id
  1024. var str = entry
  1025. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1026. row.setValue(id, forKey: "id")
  1027. //Get file
  1028. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1029. let file : String = str.subStr(start : str.indexOf(target : "\"file\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1030. row.setValue(file, forKey: "file")
  1031. //Get permalink
  1032. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1033. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  1034. row.setValue(permalink, forKey: "permalink")
  1035. //Get title_es
  1036. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1037. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1038. row.setValue(title_es, forKey: "title_es")
  1039. //Get title_en
  1040. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1041. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1042. row.setValue(title_en, forKey: "title_en")
  1043. //Get title_eu
  1044. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1045. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1046. row.setValue(title_eu, forKey: "title_eu")
  1047. //Get description_es
  1048. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1049. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1050. if (description_es != "ul"){ //From "null"
  1051. row.setValue(description_es, forKey: "description_es")
  1052. }
  1053. //Get description_en
  1054. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1055. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1056. if (description_en != "ul"){ //From "null"
  1057. row.setValue(description_en, forKey: "description_en")
  1058. }
  1059. //Get description_eu
  1060. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1061. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1062. if (description_eu != "ul"){ //From "null"
  1063. row.setValue(description_eu, forKey: "description_eu")
  1064. }
  1065. //Get uploaded
  1066. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1067. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1068. let uploaded = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"uploaded\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  1069. row.setValue(uploaded, forKey: "uploaded")
  1070. //TODO: Ignoting place
  1071. //TODO: Ignoring width
  1072. //TODO: Ignoring height
  1073. //TODO: Ignoring size
  1074. //TODO: Ignoring username
  1075. //Save CoreData
  1076. do {
  1077. try context.save()
  1078. } catch let error as NSError {
  1079. NSLog(":SYNC:ERROR: Could not store Photo with id \(id): \(error), \(error.userInfo).")
  1080. } catch {
  1081. NSLog(":SYNC:ERROR: Could not store Photo with id \(id).")
  1082. }
  1083. }
  1084. }
  1085. /**
  1086. Saves the data in the table.
  1087. :param: entries Array of strings containing the rows of the table, in JSON format.
  1088. */
  1089. func saveTablePhotoAlbum(entries : [String]){
  1090. NSLog(":SYNC:LOG: Saving table Photo_album.")
  1091. let dateFormatter = DateFormatter()
  1092. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1093. //Set up context
  1094. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1095. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1096. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1097. let entity = NSEntityDescription.entity(forEntityName: "Photo_album", in: context)
  1098. var row: NSManagedObject
  1099. //Delete all previous entries
  1100. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Photo_album")
  1101. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1102. do {
  1103. try context.execute(request)
  1104. } catch let error as NSError {
  1105. NSLog(":SYNC:ERROR: Could not clean up Photo_album: \(error), \(error.userInfo).")
  1106. } catch {
  1107. NSLog(":SYNC:ERROR: Could not clean up Photo_album entity.")
  1108. }
  1109. //Loop new entries
  1110. for entry in entries{
  1111. row = NSManagedObject(entity: entity!, insertInto: context)
  1112. //Get photo
  1113. var str = entry
  1114. let photo : Int = Int(str.subStr(start : str.indexOf(target : "\"photo\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1115. row.setValue(photo, forKey: "photo")
  1116. //Get album
  1117. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1118. let album : Int = Int(str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2))!
  1119. row.setValue(album, forKey: "album")
  1120. //Save CoreData
  1121. do {
  1122. try context.save()
  1123. } catch let error as NSError {
  1124. NSLog(":SYNC:ERROR: Could not store Photo_album entry \(error.userInfo).")
  1125. } catch {
  1126. NSLog(":SYNC:ERROR: Could not store Photo_album entry.")
  1127. }
  1128. }
  1129. }
  1130. /**
  1131. Saves the data in the table.
  1132. :param: entries Array of strings containing the rows of the table, in JSON format.
  1133. */
  1134. func saveTableFestival(entries : [String]){
  1135. NSLog(":SYNC:LOG: Saving table Festival.")
  1136. //Set up context
  1137. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1138. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1139. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1140. let entity = NSEntityDescription.entity(forEntityName: "Festival", in: context)
  1141. var row: NSManagedObject
  1142. //Delete all previous entries
  1143. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival")
  1144. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1145. do {
  1146. try context.execute(request)
  1147. } catch let error as NSError {
  1148. NSLog(":SYNC:ERROR: Could not clean up Festival: \(error), \(error.userInfo).")
  1149. } catch {
  1150. NSLog(":SYNC:ERROR: Could not clean up Festival entity.")
  1151. }
  1152. //Loop new entries
  1153. for entry in entries{
  1154. row = NSManagedObject(entity: entity!, insertInto: context)
  1155. //Get id
  1156. var str = entry
  1157. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1158. row.setValue(id, forKey: "id")
  1159. //Get year
  1160. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1161. let file: Int = Int(str.subStr(start : str.indexOf(target : "\"year\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1162. row.setValue(file, forKey: "year")
  1163. //Get text_es
  1164. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1165. let text_es: String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1166. row.setValue(text_es, forKey: "text_es")
  1167. //Get text_en
  1168. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1169. let text_en: String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1170. row.setValue(text_en, forKey: "text_en")
  1171. //Get text_eu
  1172. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1173. let text_eu: String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1174. row.setValue(text_eu, forKey: "text_eu")
  1175. //Get summary_es
  1176. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1177. let summary_es: String = str.subStr(start : str.indexOf(target : "\"summary_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1178. row.setValue(summary_es, forKey: "summary_es")
  1179. //Get summary_en
  1180. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1181. let summary_en: String = str.subStr(start : str.indexOf(target : "\"summary_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1182. row.setValue(summary_en, forKey: "summary_en")
  1183. //Get summary_eu
  1184. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1185. let summary_eu: String = str.subStr(start : str.indexOf(target : "\"summary_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1186. row.setValue(summary_eu, forKey: "summary_eu")
  1187. //Get img
  1188. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1189. let img: String = str.subStr(start : str.indexOf(target : "\"img\":")! + 7, end : str.length - 2)
  1190. row.setValue(img, forKey: "img")
  1191. //Save CoreData
  1192. do {
  1193. try context.save()
  1194. } catch let error as NSError {
  1195. NSLog(":SYNC:ERROR: Could not store Festival entry \(error.userInfo).")
  1196. } catch {
  1197. NSLog(":SYNC:ERROR: Could not store Festival entry.")
  1198. }
  1199. }
  1200. }
  1201. /**
  1202. Saves the data in the table.
  1203. :param: entries Array of strings containing the rows of the table, in JSON format.
  1204. */
  1205. func saveTableFestivalDay(entries : [String]){
  1206. NSLog(":SYNC:LOG: Saving table FestivalDay.")
  1207. let dateFormatter = DateFormatter()
  1208. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1209. //Set up context
  1210. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1211. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1212. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1213. let entity = NSEntityDescription.entity(forEntityName: "Festival_day", in: context)
  1214. var row: NSManagedObject
  1215. //Delete all previous entries
  1216. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_day")
  1217. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1218. do {
  1219. try context.execute(request)
  1220. } catch let error as NSError {
  1221. NSLog(":SYNC:ERROR: Could not clean up Festival_day: \(error), \(error.userInfo).")
  1222. } catch {
  1223. NSLog(":SYNC:ERROR: Could not clean up Festival_day entity.")
  1224. }
  1225. //Loop new entries
  1226. for entry in entries{
  1227. row = NSManagedObject(entity: entity!, insertInto: context)
  1228. //Get id
  1229. var str = entry
  1230. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1231. row.setValue(id, forKey: "id")
  1232. //Get date
  1233. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1234. dateFormatter.dateFormat = "yyyy-MM-dd"
  1235. let date = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1236. row.setValue(date, forKey: "date")
  1237. //Get name_es
  1238. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1239. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1240. row.setValue(name_es, forKey: "name_es")
  1241. //Get name_en
  1242. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1243. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1244. row.setValue(name_en, forKey: "name_en")
  1245. //Get name_eu
  1246. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1247. let name_eu: String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1248. row.setValue(name_eu, forKey: "name_eu")
  1249. //Get price
  1250. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1251. let price: Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.length - 2))!
  1252. row.setValue(price, forKey: "price")
  1253. //Save CoreData
  1254. do {
  1255. try context.save()
  1256. } catch let error as NSError {
  1257. NSLog(":SYNC:ERROR: Could not store Festival_day entry \(error.userInfo).")
  1258. } catch {
  1259. NSLog(":SYNC:ERROR: Could not store Festival_day entry.")
  1260. }
  1261. }
  1262. }
  1263. /**
  1264. Saves the data in the table.
  1265. :param: entries Array of strings containing the rows of the table, in JSON format.
  1266. */
  1267. func saveTableFestivalOffer(entries : [String]){
  1268. NSLog(":SYNC:LOG: Saving table FestivalOffer.")
  1269. //Set up context
  1270. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1271. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1272. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1273. let entity = NSEntityDescription.entity(forEntityName: "Festival_offer", in: context)
  1274. var row: NSManagedObject
  1275. //Delete all previous entries
  1276. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_offer")
  1277. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1278. do {
  1279. try context.execute(request)
  1280. } catch let error as NSError {
  1281. NSLog(":SYNC:ERROR: Could not clean up Festival_offer: \(error), \(error.userInfo).")
  1282. } catch {
  1283. NSLog(":SYNC:ERROR: Could not clean up Festival_offer entity.")
  1284. }
  1285. //Loop new entries
  1286. for entry in entries{
  1287. row = NSManagedObject(entity: entity!, insertInto: context)
  1288. // Get id
  1289. var str = entry
  1290. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1291. row.setValue(id, forKey: "id")
  1292. // Get year
  1293. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1294. let year: Int = Int(str.subStr(start : str.indexOf(target : "\"year\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1295. row.setValue(year, forKey: "year")
  1296. // Get name_es
  1297. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1298. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1299. row.setValue(name_es, forKey: "name_es")
  1300. // Get name_en
  1301. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1302. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1303. row.setValue(name_en, forKey: "name_en")
  1304. // Get name_eu
  1305. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1306. let name_eu: String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1307. row.setValue(name_eu, forKey: "name_eu")
  1308. // Get description_es
  1309. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1310. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1311. if (description_es != "ul"){ //From "null"
  1312. row.setValue(description_es, forKey: "description_es")
  1313. }
  1314. // Get description_en
  1315. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1316. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1317. if (description_en != "ul"){ //From "null"
  1318. row.setValue(description_en, forKey: "description_en")
  1319. }
  1320. // Get description_eu
  1321. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1322. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1323. if (description_eu != "ul"){ //From "null"
  1324. row.setValue(description_eu, forKey: "description_eu")
  1325. }
  1326. // Get days
  1327. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1328. let days: Int = Int(str.subStr(start : str.indexOf(target : "\"days\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1329. row.setValue(days, forKey: "days")
  1330. //Get price
  1331. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1332. let price: Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.length - 2))!
  1333. row.setValue(price, forKey: "price")
  1334. //Save CoreData
  1335. do {
  1336. try context.save()
  1337. } catch let error as NSError {
  1338. NSLog(":SYNC:ERROR: Could not store Festival_offer entry \(error.userInfo).")
  1339. } catch {
  1340. NSLog(":SYNC:ERROR: Could not store Festival_offer entry.")
  1341. }
  1342. }
  1343. }
  1344. /**
  1345. Saves the data in the table.
  1346. :param: entries Array of strings containing the rows of the table, in JSON format.
  1347. */
  1348. func saveTableFestivalEvent(entries : [String]){
  1349. NSLog(":SYNC:LOG: Saving table FestivalEvent.")
  1350. let dateFormatter = DateFormatter()
  1351. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1352. //Set up context
  1353. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1354. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1355. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1356. let entity = NSEntityDescription.entity(forEntityName: "Festival_event", in: context)
  1357. var row: NSManagedObject
  1358. //Delete all previous entries
  1359. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_event")
  1360. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1361. do {
  1362. try context.execute(request)
  1363. } catch let error as NSError {
  1364. NSLog(":SYNC:ERROR: Could not clean up Festival_event: \(error), \(error.userInfo).")
  1365. } catch {
  1366. NSLog(":SYNC:ERROR: Could not clean up Festival_event entity.")
  1367. }
  1368. //Loop new entries
  1369. for entry in entries{
  1370. row = NSManagedObject(entity: entity!, insertInto: context)
  1371. //Get id
  1372. var str = entry
  1373. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1374. row.setValue(id, forKey: "id")
  1375. //Get gm
  1376. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1377. let gm: Int = Int(str.subStr(start : str.indexOf(target : "\"gm\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1378. row.setValue(gm, forKey: "gm")
  1379. //Get title_es
  1380. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1381. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1382. row.setValue(title_es, forKey: "title_es")
  1383. //Get title_en
  1384. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1385. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1386. row.setValue(title_en, forKey: "title_en")
  1387. //Get title_eu
  1388. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1389. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1390. row.setValue(title_eu, forKey: "title_eu")
  1391. //Get description_es
  1392. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1393. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1394. if (description_es != "ul"){ //From "null"
  1395. row.setValue(description_es, forKey: "description_es")
  1396. }
  1397. //Get description_en
  1398. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1399. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1400. if (description_en != "ul"){ //From "null"
  1401. row.setValue(description_en, forKey: "description_en")
  1402. }
  1403. //Get description_eu
  1404. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1405. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1406. if (description_eu != "ul"){ //From "null"
  1407. row.setValue(description_eu, forKey: "description_eu")
  1408. }
  1409. //Get host
  1410. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1411. let strHost: String = str.subStr(start : str.indexOf(target : "\"host\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1412. if (strHost != "ul"){ //From "null"
  1413. let host: Int = Int(strHost)!
  1414. row.setValue(host, forKey: "host")
  1415. }
  1416. //Get place
  1417. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1418. let place: Int = Int(str.subStr(start : str.indexOf(target : "\"place\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1419. row.setValue(place, forKey: "place")
  1420. //Get start
  1421. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1422. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1423. let start = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1424. row.setValue(start, forKey: "start")
  1425. // Get day
  1426. // Special item, not in the sync content
  1427. dateFormatter.dateFormat = "yyyy-MM-dd"
  1428. var day = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 11))!
  1429. // If on the first hours of the next day...
  1430. let calendar = Calendar.current
  1431. let hours = calendar.component(.hour, from: day as! Date)
  1432. // ... the event belongs to the previous day.
  1433. if hours < 6{
  1434. day = Calendar.current.date(byAdding: .day, value: -1, to: day)!
  1435. }
  1436. row.setValue(day, forKey: "day")
  1437. //Get end
  1438. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1439. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1440. let strEnd: String = str.subStr(start : str.indexOf(target : "\"end\":")! + 7, end : str.length - 2)
  1441. if (strEnd != "ul"){ //From "null"
  1442. let end = dateFormatter.date(from: strEnd)!
  1443. row.setValue(end, forKey: "end")
  1444. }
  1445. //Save CoreData
  1446. do {
  1447. try context.save()
  1448. } catch let error as NSError {
  1449. NSLog(":SYNC:ERROR: Could not store Festival_event entry \(error.userInfo).")
  1450. } catch {
  1451. NSLog(":SYNC:ERROR: Could not store Festival_event entry.")
  1452. }
  1453. }
  1454. }
  1455. /**
  1456. Saves the data in the table.
  1457. :param: entries Array of strings containing the rows of the table, in JSON format.
  1458. */
  1459. func saveTableSponsor(entries : [String]){
  1460. NSLog(":SYNC:LOG: Saving table Sponsor.")
  1461. //Set up context
  1462. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1463. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1464. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1465. let entity = NSEntityDescription.entity(forEntityName: "Sponsor", in: context)
  1466. var row: NSManagedObject
  1467. //Delete all previous entries
  1468. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Sponsor")
  1469. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1470. do {
  1471. try context.execute(request)
  1472. } catch let error as NSError {
  1473. NSLog(":SYNC:ERROR: Could not clean up Sponsor: \(error), \(error.userInfo).")
  1474. } catch {
  1475. NSLog(":SYNC:ERROR: Could not clean up Sponsor entity.")
  1476. }
  1477. //Loop new entries
  1478. for entry in entries{
  1479. row = NSManagedObject(entity: entity!, insertInto: context)
  1480. //Get id
  1481. var str = entry
  1482. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1483. row.setValue(id, forKey: "id")
  1484. // Get name_es
  1485. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1486. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1487. row.setValue(name_es, forKey: "name_es")
  1488. // Get name_en
  1489. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1490. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1491. row.setValue(name_en, forKey: "name_en")
  1492. // Get name_eu
  1493. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1494. let name_eu: String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1495. row.setValue(name_eu, forKey: "name_eu")
  1496. // Get text_es
  1497. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1498. let text_es: String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1499. row.setValue(text_es, forKey: "text_es")
  1500. // Get text_en
  1501. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1502. let text_en: String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1503. row.setValue(text_en, forKey: "text_en")
  1504. // Get text_eu
  1505. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1506. let text_eu: String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1507. row.setValue(text_eu, forKey: "text_eu")
  1508. // Get image
  1509. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1510. let image: String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  1511. if (image != "ul"){ //From "null"
  1512. row.setValue(image, forKey: "image")
  1513. }
  1514. // Get address_es
  1515. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1516. let address_es: String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1517. if (address_es != "ul"){ //From "null"
  1518. row.setValue(address_es, forKey: "address_es")
  1519. }
  1520. // Get address_en
  1521. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1522. let address_en: String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1523. if (address_en != "ul"){ //From "null"
  1524. row.setValue(address_en, forKey: "address_en")
  1525. }
  1526. // Get address_eu
  1527. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1528. let address_eu: String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1529. if (address_eu != "ul"){ //From "null"
  1530. row.setValue(address_eu, forKey: "address_eu")
  1531. }
  1532. //Get link
  1533. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1534. let link: String = str.subStr(start : str.indexOf(target : "\"link\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1535. if (link != "ul"){ //From "null"
  1536. row.setValue(link, forKey: "link")
  1537. }
  1538. //Get lat
  1539. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1540. let strLat: String = str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2)
  1541. if (strLat != "ul"){ //From "null"
  1542. let lat: Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  1543. row.setValue(lat, forKey: "lat")
  1544. }
  1545. //Get lon
  1546. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1547. let strLon: String = str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2)
  1548. if (strLon != "ul"){ //From "null"
  1549. let lon: Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  1550. row.setValue(lon, forKey: "lon")
  1551. }
  1552. //Save CoreData
  1553. do {
  1554. try context.save()
  1555. } catch let error as NSError {
  1556. NSLog(":SYNC:ERROR: Could not store Sponsor entry \(error.userInfo).")
  1557. } catch {
  1558. NSLog(":SYNC:ERROR: Could not store Sponsor entry.")
  1559. }
  1560. }
  1561. }
  1562. /**
  1563. Saves the received settings.
  1564. :param: entries Array of strings containing the rows of the table, in JSON format.
  1565. */
  1566. func saveSettings(entries : [String]){
  1567. NSLog(":SYNC:LOG: Saving settings.")
  1568. let defaults = UserDefaults.standard
  1569. // Loop new entries
  1570. for entry in entries{
  1571. // Get setting name
  1572. var str = entry
  1573. let name: String = str.subStr(start : str.indexOf(target : "\"name\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1574. // Get setting value
  1575. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1576. let value: Int = Int(str.subStr(start : str.indexOf(target : "\"value\":")! + 9, end : str.length - 2))!
  1577. // Save only the needed settings
  1578. if ["festivals", "comments", "photos"].contains(name){
  1579. NSLog(":SYNC:LOG: Setting \(name) \(value)")
  1580. defaults.set(value, forKey: name)
  1581. }
  1582. }
  1583. }
  1584. }