YapDatabase sorting confounds editing update - ios

In a UITableViewController I use YapDatabase with Mantle sorting the following way:
YapDatabaseViewSorting *viewSorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(NSString *group, NSString *collection1, NSString *key1, XQBuilding *object1, NSString *collection2, NSString *key2, XQBuilding *object2) {
if ([group isEqualToString:XQBuildingsViewGroupName]) {
return [[object1 name] compare:[object2 name] options:NSNumericSearch];
}
if ([group isEqualToString:XQPicturesGroup]) {
return [[object1 updatedAt] compare:[object2 updatedAt]];
}
return NSOrderedSame;
}];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = NO;
YapDatabaseView *databaseView = [[YapDatabaseView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:#"" options:options];
Although the used option I have sometimes (when an edited name changes the edited item order in the list) incorrect indexPath on reading:
- (UITableViewCell*)editableTableView:(UITableView *)tableView simpleCellForIndexPath:(NSIndexPath *)indexPath
{
__block XQBuilding *building;
__block NSNumber *gla;
[self.readConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
building = [[transaction ext:XQBuildingListYapName] objectAtIndexPath:indexPath withMappings:self.tableViewAnimator.viewMappings];
gla = [building glaWithTransaction:transaction];
}];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BuildingCell"];
cell.textLabel.text = building.name;
cell.detailTextLabel.text = [NSString stringWithFormat: #"%# sqm", formatDecimal(gla)];
return cell;
}
i. e. building in such case is a different one than was edited. How to get a correct indexPath according to actual sorting?

There are plenty of things that should be correctly coded to make it work properly!
As I can see from your code you use YapDatabaseViewMappings. Do you use LongLivedReadTransactions and do you subscribe to YapDatabaseModifiedNotification to properly modify the table on database changes?
There is an simplified example that shows how to use all these things to update your UITableView in real time right after database is updated.

Related

Objective C delete last NSString in NSMutableArray

I am trying to get the last NSString from a NSMutableArray, and delete it if it's empty.
Here is the code I am using:
- (void)viewWillAppear:(BOOL)animated {
if ([notes.data length]==0){
[storedText removeLastObject];
}
[self.tableView reloadData];
}
data is the NSString and storedText is the NSMutableArray. This code deletes the NSString even if it's not empty. I want it to keep the string if it contains text.
You should try something like this:
- (void)viewWillAppear:(BOOL)animated {
if ([[[storedText lastObject] data] length]==0){
[storedText removeLastObject];
}
[self.tableView reloadData];
}
BTW, it seems that your hypothesis that notes.data is the same string as the last object in storedText is not correct. This leads me to suspect that you have other kinds of errors in your code that you should also investigate. In other words, what you can expect is that your notes.data is not correct -- indeed, you should at least "update" it after you remove the last object from the array:
- (void)viewWillAppear:(BOOL)animated {
if ([[[storedText lastObject] data] length]==0){
[storedText removeLastObject];
notes = [storedText lastObject]; //-- this will be the *new* last object after removal
}
[self.tableView reloadData];
}
but I have no clue what you do with notes.data, so I do not know if this by itself is enough to bring it back to being consistent with your hypothesis.
Ideally before you add any item to storedText you would check that it wasn't nil or an empty string, then you wouldn't have the problem.
Otherwise, get the last item from storedText and check it (using isEqualToString: or a length check) and then remove it if it matches.
Follow what Wain said. Do this.
//total array size
int count = [storedText count];
int lastItemIndexInt = count - 1;
//get last object in array which is a string
NSString *str1 = [storedText objectAtIndex:lastItemIndexInt];
if ([str1 isEqualToString:#""] || [str1 length] < 1)
{
//string is null / blank, remove it
[storedText removeObjectAtIndex:lastItemIndexInt];
}

Multiple methods named 'location' found with mismatched result, parameter type, or attributes

I have read the other questions concerning multiple methods but still do not know how to fix my code. I would be grateful for help with this. I have put a * around the statement where the error occurs.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"eventCell";
EQCalendarCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[EQCalendarCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.titleLabel.text = [[self.eventsList objectAtIndex:indexPath.row] title];
cell.locationLabel.text = [[self.eventsList objectAtIndex:indexPath.row] location];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat: #"dd-MM-yy HH:mm"];
NSString *startDateString = [df stringFromDate:[[self.eventsList
objectAtIndex:indexPath.row] startDate]];
cell.startDateLabel.text = startDateString;
NSString *endDateString = [df stringFromDate:[[self.eventsList
objectAtIndex:indexPath.row] endDate]];
cell.endDateLabel.text = endDateString;
return cell;
}
Thanking you in advance for your help.
Casting the result of retrieving the object from the self.eventsList collection should solve the problem. E.g.:
cell.locationLabel.text = [((MyClassName *)[self.eventsList objectAtIndex:indexPath.row]) location];
Replacing MyClassName with the name of the class in the collection.
You need to cast [self.eventsList objectAtIndex:indexPath.row] to the relevant type so that the compiler knows what data type your are dealing with.
Without seeing how your self.eventList list is populated it's not possible to tell you the solution exactly, but your line should be replaced with something like this (split into two lines for clarity, but you could use a cast instead of a variable to keep it on one line)
MyEventClass *event = [self.eventsList objectAtIndex:indexPath.row];
cell.locationLabel.text = [event location];
You need to cast EKEvent class. This will solve your problem...
EKEvent *event = [self.eventlist objectAtIndex:indexPath.row];
NSString *placeStr=[event location];
In one scenario, if the factory method that creates the object has return type "id" then the compiler will check the method signature in all the classes. If compiler find the same method signature in more than one class then it will raise the issue. So replace the return type "id" with "specific class name".

Passing parameters makes the code run "slower"?

Background Information
Currently I'm setting the text for each UITableViewCell in my UITableView using the following code:
Scenario A:
cell.textLabel.attributedText = [news formattedSubject];
However, consider if I were to add a parameter for the formattedSubject definition, just a single integer parameter so the code is now:
Scenario B:
cell.textLabel.attributedText = [news formattedSubject:1];
The text in each table view cell is roughly 3-5 lines in length, and is read from an external source and parsed via JSON. Here's a diagram of the desired result, which is what happens in Scenario A:
Scenario A Flow Diagram:
Image A simply displays the default, empty UITableView that I get when the app is still loading the JSON data. After the app retrieves and parses this data, it then populates the data into the UITableView, which results in Image B. This is the desired (and expected) result.
However, if I add a parameter to formattedSubject, I instead get the flow diagram below:
Scenario B Flow Diagram:
Once again, Image A displays the default UITableView. However, it is what happens in Image B that is the problem. In Image B, the data has been parsed, but has not yet been formatted properly by formattedSubject, thus resulting in a single, horizontally-narrow, and lengthy row of unformatted text. After a fraction of a second, the app looks like Image C, the end result which displays the formatted data after it has been parsed.
My question:
The only change I made is the addition of a parameter to formattedSubject. That is, I changed -(NSAttributedString*)formattedSubject { to -(NSAttributedString*)formattedSubject:(int)state {. It doesn't matter that there is nothing within formattedSubject that actually uses the state integer, I'm still getting the results from Scenario B.
This change seems to make the code run more slowly. It creates a delay between when the data is parsed and when it is formatted and displayed in the UITableView. I'm curious as to why this is, and how I can fix/circumvent this issue.
Aside from being an aesthetics issue, what happens in Scenario B also interferes with my automatic loading of new data when the user reaches the end of the UITableView. Because of horizontally-narrowed rows of text, the last row of data will momentarily be displayed in the UITableView when it is first loaded, thus causing data to be retrieved twice upon app startup.
I am nowhere close to an expert in coding, and thus it makes absolutely no sense to me how simply adding a parameter to my NSAttributedString could create the aforementioned delay. I would be very appreciative if someone could:
Explain why this is happening, and
Offer a solution to resolve this issue.
Thank you very much for reading this, any and all comments/help is welcomed.
Edit 1: #Vijay-Apple-Dev.blogspot.com, #txulu
Here is my formattedSubject code:
-(NSAttributedString*)formattedSubject:(int)state {
if(formattedSubject!=nil) return formattedSubject;
NSDictionary *boldStyle = [[NSDictionary alloc] init];
if(state==1) {
boldStyle = #{NSFontAttributeName:[UIFont fontWithName:#"Helvetica-Bold" size:16.0],NSForegroundColorAttributeName:[UIColor colorWithRed:0.067 green:0.129 blue:0.216 alpha:1.0]};
}
else {
boldStyle = #{NSFontAttributeName:[UIFont fontWithName:#"Helvetica-Bold" size:16.0],NSForegroundColorAttributeName:[UIColor whiteColor]};
}
NSDictionary* normalStyle = #{NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:14.0]};
NSMutableAttributedString* articleAbstract = [[NSMutableAttributedString alloc] initWithString:subject];
[articleAbstract setAttributes:boldStyle range:NSMakeRange(0, subject.length)];
[articleAbstract appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n"]];
int startIndex = [articleAbstract length];
NSTimeInterval _interval=[datestamp doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:#"MM/dd/yy"];
NSString* description = [NSString stringWithFormat:#"By %# on %#",author,[_formatter stringFromDate:date]];
[articleAbstract appendAttributedString:[[NSAttributedString alloc] initWithString: description]];
[articleAbstract setAttributes:normalStyle range:NSMakeRange(startIndex, articleAbstract.length - startIndex)];
formattedSubject = articleAbstract;
return formattedSubject;
}
Please note that as I said before, even if I don't actually use the state parameter, I still get the same results.
Here is my cellForRowAtIndexPath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
News *news = newsArray[indexPath.row];
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
if([selectedIndexPath isEqual:indexPath]) {
cell.textLabel.attributedText = [news formattedSubject:1];
}
else {
cell.textLabel.attributedText = [news formattedSubject:0];
}
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
UIView *selectedBackgroundViewForCell = [UIView new];
[selectedBackgroundViewForCell setBackgroundColor:[UIColor colorWithRed:0.169 green:0.322 blue:0.525 alpha:1.0]];
cell.selectedBackgroundView = selectedBackgroundViewForCell;
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
if (indexPath.row == [newsArray count] - 1) {
[self parseJSON];
}
return cell;
}
Please let me know if I can post anything else that may help.
Edit 2:
I'm not exactly sure if there is a performance issue. Upon further testing, I am inclined to believe that in Scenario A, the app loads and formats the cell data before displaying it, while in Scenario B, the app loads the data, displays it in the UITableViewCell, and then formats it, which creates the problem I detailed above.
Some people have brought up the code in my parseJSON method, so I'm posting it here for reference. As you can see I do indeed implement multithreading in order to prevent the data loading from lagging the application.
-(void)parseJSON
{
loading.alpha = 1;
loading.image = [UIImage imageNamed:#"loading.png"];
activityIndicator.alpha = 1;
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(checkLoading) userInfo:nil repeats:YES];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
parseNumber = parseNumber + 1;
int offset = parseNumber*20-1;
NSString *URLString = [NSString stringWithFormat:#"http://feedurl.com/feed.php?offset=%d",offset];
NSURL *url=[NSURL URLWithString:URLString];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError* error;
if(data!=nil) {
json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
for(NSDictionary *newsInfo in json) {
News *newsList = [[News alloc] init];
newsList.thread = newsInfo[#"thread"];
newsList.author = newsInfo[#"author"];
newsList.subject = newsInfo[#"subject"];
newsList.body= newsInfo[#"body"];
newsList.datestamp = newsInfo[#"datestamp"];
[jsonTemp addObject:newsList];
}
newsArray = jsonTemp;
}
dispatch_sync(dispatch_get_main_queue(), ^{
if(data!=nil) {
[newsTable reloadData];
}
else {
activityIndicator.alpha = 0;
loading.image = [UIImage imageNamed:#"error.png"];
[self startTimer];
}
});
});
}
Edit:
Okay, there's a difference when calling [news formattedSubject] instead of [news formattedSubject:1]. The first one is like doing news.formattedSubject, this is, access the formattedSubject property that returns the ivar immediately, pretty fast. The second one calls the more complex formattedSubject: method that executes the code you posted, slower.
Original:
Your code seems fine except for some minor details like:
NSDictionary *boldStyle = [[NSDictionary alloc] init];
not being necessary because you assign just afterwards:
boldStyle = #{NSFontAttributeName ...}
Also, what I guess could be causing your problem is:
if (indexPath.row == [newsArray count] - 1) {
[self parseJSON];
}
Calling this inside your cellForRowAtIndexPath: could be a severe performance problem. If this method does a lot of work and does not do it in a background it could cause the delays you mention. As a rule of thumb, you should never do network/data processing in the main thread (cellForRowAtIndexPath will always be called in that thread by the system).
You says like below
"The text in each table view cell is roughly 3-5 lines in length, and is read from an external source and parsed via JSON. Here's a diagram of the desired result, which is what happens in Scenario A:"
I assume that you are reading data from
1.Local Core data Database
or
2.Web server's database.
For case 1, you should use NSFetchedResultsController, follow up this tutorial
https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html
http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller
For case 2 you should do in background thread,and update it by Main thread in tableview, when it is available, follow up this tutorial
How to load JSON asynchronously (iOS)

Adding a logo to a data structure

I have a tableviewcell that has an UIImage for a team logo, I'm getting my data, as team name, points, etc, from a third party XML feed, which does not have the logo data included.
So I need to add this info to that data.
The problem is that i do not know how to do that.
I have my parseXML method which works, everything is working, but my uiimage for logo is being populated by a hard coded array that has all the .png files, the only thing is that if team in number1 place drops to number2, the logos won't update.
Please see my code below, I have included my logos array and the xml data structure after being parsed.
Can anyone provide a sample code of how I can achieve a solution to my problem? I've been stuck in here for a while.
-(void) parseXML{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"apikeygoeshere"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *xmlString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];
NSMutableArray *items = [xml objectForKey:#"TeamLeagueStanding"];
NSString *nullentry = #""; // custom code for specific reason
NSString *nullentry2 = #""; // custom code for a specific reason
[items insertObject:nullentry atIndex:0]; // custom code for a specific reason
[items insertObject:nullentry2 atIndex:1]; // custom code for a specific reason
[self setTableData:items];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StandingsIdent";
StandingsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
if ([item isKindOfClass:[NSDictionary class]]) {
long row = [indexPath row];
cell.cellTeamName.text = [item objectForKey:#"Team"];;
cell.cellTeamLogo.image = [UIImage imageNamed:_teamLogos[row]];
cell.cellTeamPosition.text = _teamPosition[row];
cell.cellPlayed.text = [item objectForKey:#"Played"];
cell.cellWins.text = [item objectForKey:#"Won"];
cell.cellTies.text = [item objectForKey:#"Draw"];
cell.cellLoses.text = [item objectForKey:#"Lost"]; ;
cell.cellPoints.text = [item objectForKey:#"Points"];
cell.cellInfo.text = _infoLeague[row];
}
else {
}
}
LOGO ARRAY
_teamLogos = #[#"",
#"",
#"1478.png",
#"1487.png",
#"1489.png",
#"1494.png",
#"1474.png",
#"2390.png",
#"2433.png",
#"1488.png",
#"1481.png",
#"2383.png",
#"1476.png",
#"1495.png",
#"729500.png",
#"2386.png",
#"2445.png",
#"2393.png",
#""];
XML DATA STRUCTURE AFTER BEING PARSED
Draw = 10;
"Goal_Difference" = "-17";
"Goals_Against" = 39;
"Goals_For" = 22;
Lost = 11;
NumberOfShots = 395;
Played = 25;
PlayedAtHome = 13;
PlayedAway = 12;
Points = 22;
RedCards = 5;
Team = Partick;
"Team_Id" = 561;
Won = 4;
YellowCards = 41;
}
Instead of putting your hard coded image names in an array, simply name the images the same as the team name or better yet the team id.
Then you can do:
cell.cellTeamLogo.image = [UIImage imageNamed:item[#"Team_Id"]];
This keeps things nice and simple. If you add a new team you simply add a new image with the same team id (with the .png extension added).

Add a field to each object inside an array

I'm parsing XML data of a third party provider.
They include all the information i need for each Team, except their logo.
I have all the logos imported into my project.
Is there a way to add a logo field to each team?
This is my parse method, works perfectly
-(void) parseXML{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"apikeygoeshere"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *xmlString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];
NSMutableArray *items = [xml objectForKey:#"TeamLeagueStanding"];
NSString *nullentry = #""; // custom code for specific reason
NSString *nullentry2 = #""; // custom code for a specific reason
[items insertObject:nullentry atIndex:0]; // custom code for a specific reason
[items insertObject:nullentry2 atIndex:1]; // custom code for a specific reason
[self setTableData:items];
}
This is my cellForRowAtIndexPath method, as you will see my logos are being feed from an array that i have inside my code, but if the team that is in 1st place drops to 2nd, the logos won't change position
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StandingsIdent";
StandingsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
if ([item isKindOfClass:[NSDictionary class]]) {
long row = [indexPath row];
cell.cellTeamName.text = [item objectForKey:#"Team"];;
cell.cellTeamLogo.image = [UIImage imageNamed:_teamLogos[row]];
cell.cellTeamPosition.text = _teamPosition[row];
cell.cellPlayed.text = [item objectForKey:#"Played"];
cell.cellWins.text = [item objectForKey:#"Won"];
cell.cellTies.text = [item objectForKey:#"Draw"];
cell.cellLoses.text = [item objectForKey:#"Lost"]; ;
cell.cellPoints.text = [item objectForKey:#"Points"];
cell.cellInfo.text = _infoLeague[row];
}
else {
}
}
Is it possible to somehow add these logos to each team? so when team "x" moves the logo does too.
Here is the xml data structure after being parsed:
{
Draw = 10;
"Goal_Difference" = "-17";
"Goals_Against" = 39;
"Goals_For" = 22;
Lost = 11;
NumberOfShots = 395;
Played = 25;
PlayedAtHome = 13;
PlayedAway = 12;
Points = 22;
RedCards = 5;
Team = Partick;
"Team_Id" = 561;
Won = 4;
YellowCards = 41;
}
Thanks ;)
You receive an NSDictionaryfor each team - this is an immutable object so you can't change it. But, you can create a mutable copy of it (so you will have an NSMutableDictionary).
When you do:
NSMutableArray *items = [xml objectForKey:#"TeamLeagueStanding"];
it's unlikely that you actually get a mutable array back (though you might). Again though, you can create a mutable copy if not.
Then, iterate through the teams, create a mutable copy of the dictionary, add the key/value pair that you need for the logo and then replace the original entry in the array with your updated copy.
Or, an alternate approach:
Store your logos in a different dictionary, where the keys are the team names (or unique ids) and the values are the logo image names. Then, instead of _teamLogos[row] you would use _teamLogos[[item objectForKey:#"Team"]] to get the image name.

Resources