0

I am using realm for Android. I have the following code and it works but I was wondering if it is the best way to go about updating objects and if it would cause any performance issues.

Currently, I do not want to update an existing object if the status is set to processing.

List<WorkOrderObject> woList = new ArrayList<>();
            for (int i = 0; i < openWorkOrders.size(); i++) {
                if (!visnetawrap.isUserLoggedIn) {
                    return;
                }
                WorkOrderObject wo = visnetawrap.gsonClient.fromJson(openWorkOrders.get(i).toString(), WorkOrderObject.class);

                WorkOrderObject currWO = realmThread.where(WorkOrderObject.class).equalTo("id", wo.getOrderRawId()).findFirst();
                if (currWO != null) {
                    if (currWO.getOrderStatus().equals("Processing")) {
                        continue;
                    }
                }

                issueDateTime = AppUtils.formatTimestampToDateTime(wo.getOrderIssueDate());
                issueDateString = issueDateTime.toLocalDateTime().toString("MM/dd/yyyy", Locale.US);
                dueDateTime = AppUtils.formatTimestampToDateTime(wo.getOrderDueDate());
                dueDateString = dueDateTime.toLocalDateTime().toString("MM/dd/yyyy", Locale.US);

                if (!issueDateString.equals("") && !issueDateString.equals("00/00/0000") && issueDateTime.getYear() >= now.getYear() && !dueDateString.equals("") && !dueDateString.equals("00/00/0000") && dueDateTime.getYear() >= now.getYear()) {
                    //Log.d("dueDate", dueDateString);
                    woList.add(wo);
                }
            }
            realmThread.beginTransaction();
            realmThread.copyToRealmOrUpdate(woList);
            realmThread.commitTransaction();
Joe Ginley
  • 301
  • 3
  • 15

1 Answers1

0

I think basically it is the same.
Since you are worried about performance here are ways you can improve.

private static String PROCESSING = "Processing";
private static String DATE_FORMAT = "MM/dd/yyyy";
private static String EMPTY_DATE = "00/00/0000";

public void betterMethod() {
    List<WorkOrderObject> woList = new ArrayList<>(openWorkOrders.size());

    //I think this code doesnot need to be inside loop.
    if (!visnetawrap.isUserLoggedIn) {
        return;
    }
    for (int i = 0, j = openWorkOrders.size(); i < j; i++) {
        //Since you are using gson there are ways to convert JsonArray to list directly which is a better way than this
        WorkOrderObject wo = visnetawrap.gsonClient.fromJson(openWorkOrders.get(i).toString(), WorkOrderObject.class);

        WorkOrderObject currWO = realmThread.where(WorkOrderObject.class).equalTo("id", wo.getOrderRawId()).findFirst();

        if (currWO != null && currWO.getOrderStatus().equals(PROCESSING)) { //Its cleanar way
            continue;
        }

        issueDateTime = AppUtils.formatTimestampToDateTime(wo.getOrderIssueDate());
        issueDateString = issueDateTime.toLocalDateTime().toString(DATE_FORMAT, Locale.US);
        dueDateTime = AppUtils.formatTimestampToDateTime(wo.getOrderDueDate());
        dueDateString = dueDateTime.toLocalDateTime().toString(DATE_FORMAT, Locale.US);

        //I assume you have stripped out code where it needs string
        //You can use TextUtils.isEmpty() or issueDateString.isEmpty() , 
        // issueDateString.equals("") does is creates new String which is empty and compares issueDateString with it while above methods just check the 
        //length of string
        if (!TextUtils.isEmpty(issueDateString) && !issueDateString.equals(EMPTY_DATE) && issueDateTime.getYear() >= now.getYear() && !TextUtils.isEmpty(dueDateString) && !dueDateString.equals(EMPTY_DATE) && dueDateTime.getYear() >= now.getYear()) {
            //Log.d("dueDate", dueDateString);
            woList.add(wo);
        }
    }
    if (!woList.isEmpty()) {
        realmThread.beginTransaction();
        realmThread.copyToRealmOrUpdate(woList);
        realmThread.commitTransaction();
    }
}
  1. For loop can be very large so conditional statement like currWO.getOrderStatus().equals("Processing") will create an new string and compares. It's better to initialize the string before and pass as above.
  2. Converting JsonArray to List

  3. Why instantiating arrays like new ArrayList<>(openWorkOrders.size()) and using for loop with list like for (int i = 0, j = openWorkOrders.size(); i < j; i++) {}
    Streamlining Android Apps: Eliminating Code Overhead by Jake Wharton

Community
  • 1
  • 1
subhash
  • 2,689
  • 18
  • 13
  • openWorkOrders is a JsonArray and I loop through it because if I try to convert it to to a list sometimes phones will crash, so I think looping through the jsonarray and adding each object individually is better in that situation? – Joe Ginley Oct 01 '15 at 03:02
  • The user can log out at any time so if the for is looping it will cancel if the user logged out that is why if (!visnetawrap.isUserLoggedIn) { return; } is in the loop. – Joe Ginley Oct 01 '15 at 03:03
  • I think if this runs in main thread, i think user login and this will not happen at same time. – subhash Oct 01 '15 at 03:12
  • True, I have it running in AsyncTask. – Joe Ginley Oct 01 '15 at 03:15
  • Now we may get into little deep zone which i know very less relatively that what if userlogin occurs just after check for login ? You may want to make this loop i dont know like atomic or synchronized? – subhash Oct 01 '15 at 03:23