Guest User

Untitled

a guest
Apr 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. let realm = try! Realm()
  2.  
  3. func getData(){
  4.  
  5. let dataURL = "https://api.myjson.com/bins/r9vzj"
  6.  
  7. guard let url = URL(string: dataURL) else {return}
  8.  
  9. URLSession.shared.dataTask(with: url) { (data, response, error) in
  10. guard let data = data else {return}
  11. do {
  12. //let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
  13. //print("nHERE IS THE JSON: ..",json)
  14.  
  15. let feeds = try JSONDecoder().decode(RealmFeeds.self, from: data)
  16. //print(feeds)
  17.  
  18. try! self.realm.write {
  19. self.realm.add(feeds)
  20. }
  21.  
  22. let articles = try JSONDecoder().decode([RealmArticles].self, from: data)
  23. for article in articles {
  24. try! self.realm.write {
  25. self.realm.add(article)
  26. }
  27. }
  28.  
  29. }catch let error {
  30. print(error)
  31. }
  32. }.resume()
  33. }
  34.  
  35. import Foundation
  36. import Realm
  37. import RealmSwift
  38.  
  39. class RealmFeeds: Object, Decodable {
  40.  
  41. @objc dynamic var feedTitle: String = ""
  42. var feedArticles = List<RealmArticles>()
  43.  
  44.  
  45. private enum feedCodingKeys: String, CodingKey {
  46. // Customizing key name
  47. case feedTitle = "title"
  48. case articles
  49. }
  50.  
  51. convenience init(feedTitle: String, feedArticles: List<RealmArticles>){
  52. self.init()
  53. self.feedTitle = feedTitle
  54. self.feedArticles = feedArticles
  55. }
  56.  
  57. convenience required init(from decoder: Decoder) throws {
  58. let container = try decoder.container(keyedBy: feedCodingKeys.self)
  59. let feedTitle = try container.decode(String.self, forKey: .feedTitle)
  60. let articlesArray = try container.decode([RealmArticles].self, forKey: .articles)
  61. let articlesList = List<RealmArticles>()
  62. articlesList.append(objectsIn: articlesArray)
  63. self.init(feedTitle: feedTitle, feedArticles: articlesList)
  64.  
  65. }
  66.  
  67. required init() {
  68. super.init()
  69. }
  70.  
  71. required init(value: Any, schema: RLMSchema) {
  72. super.init(value: value, schema: schema)
  73. }
  74.  
  75. required init(realm: RLMRealm, schema: RLMObjectSchema) {
  76. super.init(realm: realm, schema: schema)
  77. }
  78.  
  79. }
  80.  
  81. import Foundation
  82. import Realm
  83. import RealmSwift
  84.  
  85.  
  86. class RealmArticles: Object, Codable {
  87.  
  88. @objc dynamic var articleTitle: String = ""
  89. @objc dynamic var website: String = ""
  90. @objc dynamic var authors: String = ""
  91. @objc dynamic var date: String = ""
  92. @objc dynamic var content: String = ""
  93. @objc dynamic var imageUrl: String = ""
  94.  
  95.  
  96. private enum ArticlesCodingKeys: String, CodingKey {
  97.  
  98. // Customizing some key names
  99. case articleTitle = "title"
  100. case website
  101. case authors
  102. case date
  103. case content
  104. case imageUrl = "image_url"
  105. }
  106.  
  107. convenience init(articleTitle: String, website: String, authors: String, date: String, content: String, imageUrl: String){
  108. self.init()
  109. self.articleTitle = articleTitle
  110. self.website = website
  111. self.authors = authors
  112. self.date = date
  113. self.content = content
  114. self.imageUrl = imageUrl
  115. }
  116.  
  117.  
  118. convenience required init(form decoder: Decoder) throws {
  119. let container = try decoder.container(keyedBy: ArticlesCodingKeys.self)
  120. let articleTitle = try container.decode(String.self, forKey: .articleTitle)
  121. let website = try container.decode(String.self, forKey: .website)
  122. let authors = try container.decode(String.self, forKey: .authors)
  123. let date = try container.decode(String.self, forKey: .date)
  124. let content = try container.decode(String.self, forKey: .content)
  125. let imageUrl = try container.decode(String.self, forKey: .imageUrl)
  126.  
  127. self.init(articleTitle: articleTitle, website: website, authors: authors, date: date, content: content, imageUrl: imageUrl)
  128. }
  129.  
  130.  
  131. required init() {
  132. super.init()
  133. }
  134.  
  135. required init(value: Any, schema: RLMSchema) {
  136. super.init(value: value, schema: schema)
  137. }
  138.  
  139. required init(realm: RLMRealm, schema: RLMObjectSchema) {
  140. super.init(realm: realm, schema: schema)
  141. }
  142.  
  143.  
  144. }
Add Comment
Please, Sign In to add comment