xcode - Should I have used a CoreData predicate to populate my UITableview? -


i had old app rebuilt using coredata , swift 2. first go @ coredata, i'm sure i'm not doing efficient way possible. example, have 2 entities in coredata, person entity , statement entity. person entity has relationship of "to many" statement entity, statement entity has relationship of "to one" person entity. person object able have multiple statement objects. working , able add them coredata successfully.

next have uiviewcontroller uitableview inside of it. have tableview's selection dropdown set multiple sections. have 2 sections assigned table view.

when grab coredata entities place them array able use array , fill tableview.

 var persons: [person]!  persons = try coredatastack.context.executefetchrequest(fetchrequest) as! [person] 

after (and think things simplier) break array 2 different arrays each section in table view has it's own data source. think use coredata nspredicate, after looking @ tutorial , reading couple of chapters of book have on coredata, still don't understand how use them. instead i'm doing make 2 different arrays array created coredata.

for(var = 0; persons?.count > i; i++){         newnumber = 0.00         print("\(persons?[i].statements?.count) statements count")          if(persons?[i].statements?.count > 0){              print("yes statements")             //persons[i].statements             for(var x = 0; persons[i].statements?.count > x; x++){                  //print( "\(persons[i].statements?[x].name) name " )                 let statementnumber = persons[i].statements?[x] as? statement                 //print(statementnumber?.amount)                  newnumber = newnumber + double((statementnumber?.amount)!)                 //print(newnumber)             }             // check new number , see if it's positive or negative             if(newnumber >= 0){                 positiveearray.append(persons[i])                 //print("\(positivearray.count) count of posve array")             }             else{                 negativearray.append(persons[i])                 //print("\(negativearray.count) count of negative array")             }          }else{              print("there no statements add person positive array")             positivearray.append(persons[i])         }     } 

basically each statement entity has property of amount. want check see if property positive or negative number. again system works, seems it's ton of hoops jump through access data.

after take these 2 arrays (positive & negative) , use them populate different sections of uitableview.

func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {      let cell = tableview.dequeuereusablecellwithidentifier("personcell", forindexpath: indexpath) as! persontableviewcell      //print("\(persons?[indexpath.row].statements?.count) count use")     //print("\(indexpath.row) index row")      var numberfordispay = 0.00      switch(indexpath.section) {     case 0:         if(positivearray[indexpath.row].statements?.count != 0){              for(var = 0; positivearray[indexpath.row].statements?.count > i; i++){                  let statementnumber = positivearray[indexpath.row].statements?[i] as? statement                 //print(statementnumber?.amount)                 numberfordispay = numberfordispay + double((statementnumber?.amount)!)                  // make negative numbers positive                 numberfordispay = fabs(numberfordispay)                  let formatter = nsnumberformatter()                 formatter.numberstyle = .currencystyle                  cell.statementamount.text = formatter.stringfromnumber(numberfordispay)                 //print("\(formatter) number use")             }         }else{             cell.statementamount.text = "$0.00"         }     case 1:         if(negativearray[indexpath.row].statements?.count != 0){              for(var = 0; negativearray[indexpath.row].statements?.count > i; i++){                  let statementnumber = negativearray[indexpath.row].statements?[i] as? statement                 //print(statementnumber?.amount)                 numberfordispay = numberfordispay + double((statementnumber?.amount)!)                  // make negative numbers positive                 numberfordispay = fabs(numberfordispay)                  let formatter = nsnumberformatter()                 formatter.numberstyle = .currencystyle                  cell.statementamount.text = formatter.stringfromnumber(numberfordispay)                 //print("\(formatter) number use")             }         }else{             cell.statementamount.text = "$0.00"         }     default: ""     }      switch(indexpath.section) {         case 0:             cell.personname.text = postitivearray[indexpath.row].name             cell.backgroundcolor = styles.bluecolor();         case 1:             cell.personname.text = negativearray[indexpath.row].name             cell.backgroundcolor = styles.redcolor();          default: cell.personname.text = "hello"     }      cell.personname.textcolor = styles.whitecolor()     cell.statementamount.textcolor = styles.whitecolor()      // make divide line full width     cell.separatorinset = uiedgeinsetsmake(0, 0, cell.frame.size.width, 0)     if (cell.respondstoselector("preservessuperviewlayoutmargins")){         cell.layoutmargins = uiedgeinsetszero         cell.preservessuperviewlayoutmargins = false     }      // set accessory icon color     cell.tintcolor = uicolor.whitecolor()      return cell } 

again works, i'm hoping i'm able clean amount of code have , streamline process. coming flash / as3 background. know long post, if got far, thank you! want make sure i'm not creating more work myself when work on these apps.

i recommend using nsfetchedresultscontroller. use two, 1 each section. lot of optimizations in memory, database access , performance.

here 2 predicates, add fetch request of fetched results controllers (fetching statement objects):

nspredicate(format: "amount < 0") nspredicate(format: "amount >= 0") 

return 2 number of sections, , fetchedobjects.count of corresponding fetched results controller numberofrowsinsection.

you single fetched results controller well. have hard-code database flag indicating section each amount belongs to. whenever set amount, automatically have entity object set plus/minus flag.

btw, rather iterating through items, can more concise in swift (not managed objects kind of object). e.g.

let negative = allstatements.filter { $0.amount < 0 } let positive = allstatements.filter { $0.amount >= 0 } 

note there aggregate functions available core data. sum of statement amounts of person be

let sum = person.valueforkeypath("@sum.amount") as! nsnumber 

with technique, have sum attribute of person updated whenever set amount of statement.

so person predicates be

nspredicate(format: "sum < 0") nspredicate(format: "sum >= 0") 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -