Currently I am checking whether the object already exists in core data based on id and then updating and inserting. Is there any better way to do it? Have added "id" as a unique constraint, Which prevents inserting of objects with same "id". Does inserting just update the existing object with same id?
#nonobjc public class func saveUserMovies(movieJSON: [[String: Any]], user: UserProfile, isFavorites: Bool = false, isWatchlisted: Bool = false) {
let context = MMPersistentStore.sharedInstance.privateManagedObjectContext
for movie in movieJSON {
let movieID = movie["id"] as! Int
let fetchMovieWithIDRequest = fetchMovieRequest()
let moviePredicate = NSPredicate(format: "id == %d", movieID)
let sortDiscriptor = NSSortDescriptor(key: "id", ascending: false)
fetchMovieWithIDRequest.sortDescriptors = [sortDiscriptor]
fetchMovieWithIDRequest.predicate = moviePredicate
var userMovie: UserMovie?
context.performAndWait {
do {
userMovie = try fetchMovieWithIDRequest.execute().first
} catch {
print(MMErrorStrings.coreDataFetchError)
}
}
if let fetchedMovie = userMovie {
fetchedMovie.genreIds = movie["genre_ids"] as? [Int64]
fetchedMovie.adult = movie["adult"] as? Bool ?? false
if isFavorites {
fetchedMovie.isFavorite = isFavorites
} else {
fetchedMovie.isWatchlisted = isWatchlisted
}
fetchedMovie.video = movie["video"] as? Bool ?? false
fetchedMovie.backdropPath = movie["backdrop_path"] as? String
fetchedMovie.originalLanguage = movie["original_language"] as? String
fetchedMovie.originalTitle = movie["original_title"] as? String
fetchedMovie.overview = movie["overview"] as? String
fetchedMovie.posterPath = movie["poster_path"] as? String
fetchedMovie.releaseDate = movie["release_date"] as? String
fetchedMovie.releaseYear = String(fetchedMovie.releaseDate?.prefix(4) ?? "")
fetchedMovie.title = movie["title"] as? String
fetchedMovie.popularity = movie["popularity"] as? Double ?? 0.0
fetchedMovie.voteCount = movie["voteCount"] as? Int64 ?? 0
fetchedMovie.voteAverage = movie["voteAverage"] as? Double ?? 0.0
MMPersistentStore.sharedInstance.save(context: context)
} else {
let fetchedMovie = UserMovie(context: context)
fetchedMovie.id = movie["id"] as? Int64 ?? 0
fetchedMovie.user = user
fetchedMovie.genreIds = movie["genre_ids"] as? [Int64]
fetchedMovie.adult = movie["adult"] as? Bool ?? false
if isFavorites {
fetchedMovie.isFavorite = isFavorites
} else {
fetchedMovie.isWatchlisted = isWatchlisted
}
fetchedMovie.video = movie["video"] as? Bool ?? false
fetchedMovie.backdropPath = movie["backdrop_path"] as? String
fetchedMovie.originalLanguage = movie["original_language"] as? String
fetchedMovie.originalTitle = movie["original_title"] as? String
fetchedMovie.overview = movie["overview"] as? String
fetchedMovie.posterPath = movie["poster_path"] as? String
fetchedMovie.releaseDate = movie["release_date"] as? String
fetchedMovie.releaseYear = String(fetchedMovie.releaseDate?.prefix(4) ?? "")
fetchedMovie.title = movie["title"] as? String
fetchedMovie.popularity = movie["popularity"] as? Double ?? 0.0
fetchedMovie.voteCount = movie["voteCount"] as? Int64 ?? 0
fetchedMovie.voteAverage = movie["voteAverage"] as? Double ?? 0.0
MMPersistentStore.sharedInstance.save(context: context)
}
}
}
}
Have added "id" as a unique constraint, Which prevents inserting of objects with same "id".
I didn't use it before yet
Does inserting just update the existing object with same id?
No, it'll insert the new object.
For your case, you could make a refactoring, please refer the findOrCreate function in https://github.com/objcio/core-data/blob/master/SharedCode/Managed.swift
It'll help you avoid duplicated code.
One more thing, your request doesn't need the sortDescriptor, and it should have the limit = 1, and returnObjectAsFaults = false for optimisation.
After that, you just need to make sure your function is called in the same context to avoid duplications.
I have the following array which is passed from an API call from a PHP Script:
["playerForm": {
1 = (
{
date = "2017-01-31";
name = Dicky;
result = L;
"results_id" = 42;
},
{
date = "2016-12-24";
name = Dicky;
result = W;
"results_id" = 19;
}
);
2 = (
{
date = "2017-01-25";
name = G;
result = W;
"results_id" = 38;
},
{
date = "2017-01-25";
name = G;
result = D;
"results_id" = 40;
},
{
date = "2017-01-21";
name = G;
result = L;
"results_id" = 34;
}
);
3 = (
{
date = "2017-01-31";
name = Sultan;
result = W;
"results_id" = 42;
},
{
date = "2017-01-15";
name = Sultan;
result = L;
"results_id" = 30;
}
);
}]
However I cannot seem to access the 1,2,3 parts of it...
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let dict = json?["playerForm"] as? [String:AnyObject] {
print ("step 1")
if let arr = dict["1"] as? [[String:String]] {
print ("step 2")
self.leagueForm = arr.flatMap { Form($0) }
print (self.leagueForm)
print (self.leagueForm.count)
for i in 0..<self.leagueForm.count {
let form = self.leagueForm[i]
print (form.player_results!)
self.formGuide.append(form.player_results!)
}
print ("now")
print (self.formGuide)
self.resultsDisplay.results = self.formGuide
self.resultsDisplay.results = self.resultsDisplay.results.reversed()
self.resultsDisplay.displayResults()
}
}
With this code above it only gets as far as Step 1.
What am I doing wrong?
UPDATED WITH PROGRSS*
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let dict = json?["playerForm"] as? [String:AnyObject] {
print ("step 1")
for (_ , value) in dict {
if let arr = value as? [[String:Any]] {
print(arr)
//your code
}
}
}
Custom Struct to define array:
struct Form {
var player_result: String?
var player_name: String?
var result_date: String?
var result_id: String?
init(_ dictionary: [String : Any]) {
self.player_result = dictionary["result"] as? String ?? ""
self.player_name = dictionary["name"] as? String ?? ""
result_date = dictionary["date"] as? String ?? ""
result_id = String(dictionary["results_id"] as? Int ?? 0)
}
}
Change your array type [[String:String]] to [[String:Any]] because it contains both String and Number as value.
if let arr = dict["1"] as? [[String:Any]] {
print(arr)
//your code
}
Note: You need to loop through the dict Dictionary because its each key having array.
for (_ , value) in dict {
if let arr = value as? [[String:Any]] {
print(arr)
//your code
}
}
I am trying to parse and get the values from this structure of JSON:
["mst_customer": 1, "data": {
0 = 2;
1 = 1;
2 = 1;
3 = "JAYSON TAMAYO";
4 = "581-113-113";
5 = 56;
6 = on;
7 = g;
8 = jayson;
9 = active;
"app_access" = on;
id = 2;
"mst_customer" = 1;
name = "Jayson Tamayo";
status = active;
territory = 1;
}, "status": OK, "staff_id": 2, "staff_name": Jayson Tamayo]
I use the following Swift code to get the values:
(data: Dictionary<String, AnyObject>, error: String?) -> Void in
if error != nil {
print(error)
} else {
if let feed = data["data"] as? NSDictionary ,let entries = data["data"] as? NSArray{
for elem: AnyObject in entries{
if let staff_name = elem["name"] as? String{
print(staff_name)
}
}
}
}
I am trying to get the name by using the keys name or staff_name. But I always get nil.
you want to access staff_name, which is not in "data" variable ... you can simply get that like
if error != nil {
print(error)
} else {
if let name = data["staff_name"] as? String{
print(name)
}
}
for elem: AnyObject in entries{
if let songName = elem["name"] as? String{
print(songName)
}
}
//replace above code with below code
if let songName : String = entries["name"] as? String{
print(songName)
}
{
QueueId = 27;
SongId = 38;
artWorkURL = "<null>";
duration = 58258;
"stream_url" = "https://api.soundcloud.com/tracks/233301835/stream";
title = Magenta;
trackID = 233301835;
userAvatar = "https://i1.sndcdn.com/avatars-000188204071-llusgk-large.jpg";
userName = Agiv;
},
{
QueueId = 27;
SongId = 39;
artWorkURL = "<null>";
duration = 79000;
"stream_url" = "https://api.soundcloud.com/tracks/233301833/stream";
title = Nino;
trackID = 233301833;
userAvatar = "https://i1.sndcdn.com/avatars-000157591669-eva3mg-large.jpg";
userName = "SWR Umwelt und Ern\U00e4hrung";
}
My array of dictionary format is as above and multiple tracks i want to check 27 is already there or not ?
You can do this with the filter function
let queueID27Exists = !array.filter({$0["QueueId"] as? Int == 27}).isEmpty
This answer is for your previous JSON object!
if let results : NSDictionary = post.objectForKey("data") as? NSDictionary {
let array:NSArray = (results.valueForKey("track") as! NSArray)
if "25" == array[0].valueForKey("trackID") as? String {
NSLog("YES")
} else {
NSLog("NO")
}
}
var found = false
for (_, data) in post {
let track = data["track"]
if track["trackID"] == "25" {
found = true
}
I have EKParticipant object with description looks like:
item description: EKAttendee <0x1c0b7d90> {UUID = 116B99AB9-41AC-4741-A288-B67172298625; name = Snaggy Snags; email = snaggy#gmail.com; status = 4; role = 1; type = 1}
How to safety split this string to Dictionary in order to fetch after email key value?
This is what I did so far:
extension String {
func split(splitter: String) -> Array<String> {
let regEx = NSRegularExpression(pattern: splitter, options: NSRegularExpressionOptions(), error: nil)!
let stop = "SomeStringThatYouDoNotExpectToOccurInSelf"
let modifiedString = regEx.stringByReplacingMatchesInString(self, options: NSMatchingOptions(), range: NSMakeRange(0, countElements(self)), withTemplate: stop)
return modifiedString.componentsSeparatedByString(stop)
}
func removeCharsFromEnd(count:Int) -> String{
let stringLength = countElements(self)
let substringIndex = (stringLength < count) ? 0 : stringLength - count
return self.substringToIndex(advance(self.startIndex, substringIndex))
}
}
var str = "item description: EKAttendee <0x1c0b7d90> {UUID = 16B99AB9-41AC-4742-A288-B67172299625; name = Snaggy Snags; email = snaggy#gmail.com; status = 4; role = 1; type = 1}"
var newStr = str.split("\\{")[1]
newStr = newStr.removeCharsFromEnd(1)
So now newStr equals:
UUID = 16B99AB9-41AC-4741-A288-B67172298625; name = Snaggy Snags; email = snaggy#gmail.com; status = 4; role = 1; type = 1
What next?
Thanks,
You can use componentsSeparatedByString method to extract your elements as follow:
extension String {
var elements:(udid: String, name: String, email: String, status: Int, role: Int, type: Int) {
let components = componentsSeparatedByString("; ")
if components.count == 6 {
let udid = components[0].componentsSeparatedByString(" = ").last ?? ""
let name = components[1].componentsSeparatedByString(" = ").last ?? ""
let email = components[2].componentsSeparatedByString(" = ").last ?? ""
let status = components[3].componentsSeparatedByString(" = ").last ?? ""
let role = components[4].componentsSeparatedByString(" = ").last ?? ""
let type = components[5].componentsSeparatedByString(" = ").last ?? ""
return (udid, name, email, (status as NSString).integerValue, (role as NSString).integerValue, (type as NSString).integerValue)
}
return ("","","",0,0,0)
}
}
let input = "UUID = 16B99AB9-41AC-4741-A288-B67172298625; name = Snaggy Snags; email = snaggy#gmail.com; status = 4; role = 1; type = 1"
let result = input.elements // (.0 "16B99AB9-41AC-4741-A288-B67172298625", .1 "Snaggy Snags", .2 "snaggy#gmail.com", .3 4, .4 1, .5 1, .6 "UUID = 16B99AB9-41AC-4741-A288-B67172298625; name = Snaggy Snags; email = snaggy#gmail.com; status = 4; role = 1; type = 1")
println(result.udid) // "16B99AB9-41AC-4741-A288-B67172298625"
println(result.name) // "Snaggy Snags"
println(result.email) // "snaggy#gmail.com"
println(result.status.description) // "4"
println(result.role.description) // "1"
println(result.type.description) // "1"
You can also use String's method hasPrefix to make sure you are grabbing the right info from your elements even if they return unordered as follow:
extension String {
var elements:(udid: String, name: String, email: String, status: Int, role: Int, type: Int) {
let components = componentsSeparatedByString("; ")
var udid = "", name = "", email = "", status = 0, role = 0, type = 0
for item in components {
println(item)
if item.hasPrefix("UUID = "){
udid = item.substringWithRange(Range(start: advance(item.startIndex, 7), end: item.endIndex))
}
if item.hasPrefix("name = "){
name = item.substringWithRange(Range(start: advance(item.startIndex, 7), end: item.endIndex))
}
if item.hasPrefix("email = "){
email = item.substringWithRange(Range(start: advance(item.startIndex, 8), end: item.endIndex))
}
if item.hasPrefix("status = "){
status = (item.substringWithRange(Range(start: advance(item.startIndex, 9), end: item.endIndex)) as NSString).integerValue
}
if item.hasPrefix("role = "){
role = (item.substringWithRange(Range(start: advance(item.startIndex, 7), end: item.endIndex)) as NSString).integerValue
}
if item.hasPrefix("type = "){
type = (item.substringWithRange(Range(start: advance(item.startIndex, 7), end: item.endIndex)) as NSString).integerValue
}
}
return (udid, name, email, status, role, type)
}
}
let input = "UUID = 16B99AB9-41AC-4741-A288-B67172298625; name = Snaggy Snags; email = snaggy#gmail.com; status = 4; role = 1; type = 1"
let elements = input.elements // (.0 "16B99AB9-41AC-4741-A288-B67172298625", .1 "Snaggy Snags", .2 "snaggy#gmail.com", .3 4, .4 1, .5 1)
let udid = elements.udid // "16B99AB9-41AC-4741-A288-B67172298625"
let name = elements.name // "Snaggy Snags"
let email = elements.email // "snaggy#gmail.com"
let status = elements.status.description // "4"
let role = elements.role.description // "1"
let type = elements.type.description // "1"
Here is method that fetches email or returns nil if something goes wrong:
func fetchEmailIfExists(str:String) -> String?{
var email:String?
for item:String in str.split(";"){
if item.contains("email"){
var emailPart = item.trim()
if emailPart.componentsSeparatedByString("=").first?.trim() == "email" {
if let temp:String = emailPart.componentsSeparatedByString("=").last?.trim(){
return temp
}
}
}
}
return email
}
Helpers
extension String {
func split(splitter: String) -> Array<String> {
let regEx = NSRegularExpression(pattern: splitter, options: NSRegularExpressionOptions(), error: nil)!
let stop = "SomeStringThatYouDoNotExpectToOccurInSelf"
let modifiedString = regEx.stringByReplacingMatchesInString(self, options: NSMatchingOptions(), range: NSMakeRange(0, countElements(self)), withTemplate: stop)
return modifiedString.componentsSeparatedByString(stop)
}
func trim() -> String {
return self.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())
}
func contains(find: String) -> Bool{
return self.rangeOfString(find) != nil
}
}
This a perfect use case for NSScanner. The stringToDictionary takes a String like yours and returns a dictionary of [String: String]. You can use it for any string in your format, regardless of the amount of key/value pairs. However it will break down when the values contain semicolons or equal signs.
let string = "item description: EKAttendee <0x1c0b7d90> {UUID = 116B99AB9-41AC-4741-A288-B67172298625; name = Snaggy Snags; email = snaggy#gmail.com; status = 4; role = 1; type = 1}"
var dictionary = stringToDictionary(string)
func stringToDictionary(input: String) -> [String: String] {
var output = [String: String]()
let scanner = NSScanner(string: input)
let separatingCharacters = NSCharacterSet(charactersInString: ";}")
scanner.scanUpToString("{", intoString: nil)
scanner.scanString("{", intoString: nil)
var key: NSString?, value: NSString?
while !scanner.atEnd {
scanner.scanUpToString(" =", intoString: &key)
scanner.scanString("= ", intoString: nil)
scanner.scanUpToCharactersFromSet(separatingCharacters, intoString: &value)
scanner.scanCharactersFromSet(separatingCharacters, intoString: nil)
if let key = key as? String, value = value as? String {
output[key] = value
}
}
return output
}