Claude31 says
Do it here (and test with the print):
The function is still not being called.
Post
Replies
Boosts
Views
Activity
szymczyk says
Make sure the saveContext function is called by setting a breakpoint on that line of code. Does it get called in applicationWillTerminate?
I made a mistake in my first reply. For an iOS app the function where you should save the context is applicationDidEnterBackground. That's the function that will be called when someone quits the app. The applicationWillTerminate function does not get called until the app is purged from memory completely, which requires the user to launch several apps after quitting your app.
I tried your solution but it still does not save the favourites when it terminates and the function does not get called at all.
func applicationDidEnterBackground(_ application: UIApplication) {
PersistenceService.saveContext()
}
szymczyk says
Can you explain what you mean by "make the object persistent"? Saving the context is how you save data in Core Data.
I'm thinking that I have to initialize my item object with NSManagedContext so that it can be saved locally to the device.
Okay I have the function you are talking about in AppDelegate but it does not save my data when I terminate the app. Are you sure I don't have to make the object persistent?
func applicationWillTerminate(_ application: UIApplication) {
/* Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground*/
PersistenceService.saveContext()
}
@Claude
You say:
Please, when you answer and there are several posts, please remind who you are replying to.
Your solution works and it is currentFav but I want to make a minor modification this piece of code pushes my HdVC to FavVC but I'm wondering if there's a way I can have my data be transferred without navigating to FaVC (just stay at HdVC but transfer data to FaVC)?
self.present(passDataVC, animated: true, completion: nil)
Okay so your solution dismisses my HdVC but when it goes to FaVC it doesn't seem to have the data. I just see a white screen which means my transferred item is nil still.
Yes you described my view hierarchy perfectly. I'm going to try your solution.
My common ancestor VC is connected to my HdVC through a segue and my FaVC through a bar button item and is held by a container nav controller.
Honestly I think the only way to do it would be broadcasting with NotificationCenter.
The issue with push is because I want to stay on the same initial screen. Like with facebook when you get a friend request and add a friend you don't go to your all friends section when you click add friend, you stay on the same screen as the friend request. That's what I want to do with adding to my favVC.
Thanks for your help. I just noticed Jersey in the json it must of been changed recently.
I will try to clarify it more I'm using a button to add it to my another VC I call my favVC but the kicker is I don't want to push to my favVC. I have a separate button which will go to my favVC and push my initial VC.
This is an example of a json object, I can print it but it just does not seem to parse anymore. I had to remove some url fields.
{
BirthCity = Pittsburgh;
BirthDate = "1993-07-11T00:00:00";
BirthState = PA;
Catches = "null";
DepthChartOrder = 5;
DepthChartPosition = C;
DraftKingsName = "Vincent Trocheck";
DraftKingsPlayerID = 607956;
FanDuelName = "Vincent Trocheck";
FanDuelPlayerID = 15283;
FantasyAlarmPlayerID = 400963;
FantasyDraftName = "Vincent Trocheck";
FantasyDraftPlayerID = 1818113;
FirstName = Vincent;
GlobalTeamID = 30000009;
Height = 70;
InjuryBodyPart = Scrambled;
InjuryNotes = Scrambled;
InjuryStartDate = "2021-03-18T00:00:00";
InjuryStatus = Scrambled;
Jersey = 16;
LastName = Trocheck;
PlayerID = 30000226;
Position = C;
RotoWirePlayerID = 3784;
RotoworldPlayerID = 3768;
Shoots = R;
SportRadarPlayerID = "16bf9f68-95f9-4789-a811-80fe6838e632";
SportsDirectPlayerID = 4433;
StatsPlayerID = 607956;
Status = Active;
Team = CAR;
TeamID = 9;
UsaTodayPlayerID = 8256334;
Weight = 183;
XmlTeamPlayerID = 19046;
YahooName = "Vincent Trocheck";
YahooPlayerID = 5431;
}
Okay just one more thing, I'm trying to handle the nil values in my jerseyNumber but when I do my guard statement it somehow blocks the json from being parsed. Is there another way to deal with nil values from jerseyNumber?
struct ApiCurrentPlayers: Decodable {
var PhotoUrl: String?
var FirstName: String?
var LastName: String?
var Position: String?
var Team: String?
var YahooName: String?
var Status: String?
var JerseyNumber: Int64?
}
extension CurrentPlayers {
convenience init?(context: NSManagedObjectContext, apiCurrentPlayers: ApiCurrentPlayers) {
guard let yahooName = apiCurrentPlayers.YahooName else {
return nil
}
guard apiCurrentPlayers.JerseyNumber != nil else {
return nil
}
self.init(context: context)
self.photoUrl = apiCurrentPlayers.PhotoUrl ?? ""
self.firstName = apiCurrentPlayers.FirstName ?? ""
self.lastName = apiCurrentPlayers.LastName ?? ""
self.position = apiCurrentPlayers.Position ?? ""
self.team = apiCurrentPlayers.Team ?? ""
self.yahooName = yahooName
self.status = apiCurrentPlayers.Status ?? ""
self.jerseyNumber = apiCurrentPlayers.JerseyNumber!
}
}
It's an API issue I want to filter them when I parse the json.
I filter players based on a certain position using a segmented controller and I see that there are players that have no name in my collection view so I'm thinking since I used my original array to filter them that the problem must be with the API getting the empty names, the json has names with null.