안녕하세요^^ 즐거운 금요일인데 다들 잘 보내시고 있으신지요.. ㅎㅎ

 

다름이 아니라 매번 개발 할때마다 문제가 되는 속도 문제 때문에 이번에도 크게 말썽을 부리네요..ㅜㅜ

 

xml로 2만 8천건의 데이터를 받아오는데요 .. 이거를 또 안드로이드 로컬에 있는 DB(table) 에 두번씩 저장을 해야되요.. 가뜩이나 느린데 말이죠..

그래서 초보의 실력으로 작업을 해서 그런지 몰라도.. 데이터 받고 저장하는 시간이 빠름면 4분 느리면 5~6분정도 어마어마한 시간이 걸립니다.

 

xml 같은 경우는 연동 자체는 시간이 별로 안거릴는거 같은데요 데이터를 체크하고 정보를 받아오는데 시간이 많이 걸리는 것 같습니다.

 

  public void goStart(){
     try {
      db = DBHandler.open(this);
   URL url = new URL("http://www.*****.com/external/****_firstxml.php?code=%@");
   XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
   XmlPullParser parser = factory.newPullParser();
   parser.setInput(url.openStream(), "utf-8");
   
   /* DB 업데이트  */
   
   boolean setResult = false;
   
   int eventType = parser.getEventType();
   
   int i = 0;
   String tag = "";
   Item item = null;
   while(eventType != XmlPullParser.END_DOCUMENT){
    switch (eventType) {
    case XmlPullParser.START_DOCUMENT:
     break;
    case XmlPullParser.END_DOCUMENT:
     break;
    case XmlPullParser.START_TAG:
     tag = parser.getName();
     setResult = true;
     break;
    case XmlPullParser.END_TAG:
     if(tag.equals("distance")){
      items.add(item);
      tag = "";
     }
     break;
    case XmlPullParser.TEXT:
     if (setResult) {
      if (tag.equals("ID")) {
       item = new Item();
       item.setID(parser.getText().trim());
      } else if (tag.equals("couponID")) {
       item.setCouponID(parser.getText().trim());
      } else if (tag.equals("shopID")) {
       item.setShopID(parser.getText().trim());
      } else if (tag.equals("shopname")) {
       item.setShopname(parser.getText().trim());
      } else if (tag.equals("shopmaplat")) {
       item.setShopmaplat(parser.getText().trim());
      } else if (tag.equals("shopmaplng")) {
       item.setShopmaplng(parser.getText().trim());
      } else if (tag.equals("shopcategory")) {
       item.setShopcategory(parser.getText().trim());
      } else if (tag.equals("coupontitle")) {
       item.setCoupontitle(parser.getText().trim());
      } else if (tag.equals("couponsaleprice")) {
       item.setCouponsaleprice(parser.getText().trim());
      } else if (tag.equals("couponbuyend")) {
       item.setCouponbuyend(parser.getText().trim());
      } else if (tag.equals("distance")) {
       item.setDistance(parser.getText().trim());
      }
     }
     setResult = false;
     break;
    default:
     break;
    }
    eventType = parser.next();
   }
   /* 매장 ZSHOP을 제외한 나머지는 데이터 초기화  */
   db.delete("ZCOUPON");
   db.delete("ZRELATION");
  } catch (Exception e) {
   // TODO: handle exception
   Toast.makeText(this, e.getMessage(), 0).show();
  }
  
  for (Item item: items) {
   /* 새로 받은 데이터 저장 */
   db.insertZCOUPON("ZCOUPON", item.getID(), item.getCoupontitle(), item.getCouponsaleprice(), item.getCouponbuyend(), item.getDistance());
   db.insertZRELATION("ZRELATION", item.getID(), item.getCouponID(), item.getShopID());
   
   /* 매장 정보 조회 */
   cr = db.findCnt("ZSHOP", item.getShopID());
   startManagingCursor(cr);
   shop_count = cr.getCount();
   
   /* 이미 매장이 있을 경우 저장 X 없는 경우만  */
    db.insertZSHOP("ZSHOP", item.getID(), item.getShopname(), item.getShopmaplat(), item.getShopmaplng(), item.getShopcategory(), item.getDistance());
    /*Log.v("Show Xml Data Parsing Value", 
     "ID : " + item.getID() 
     + ", couponID : " + item.getCouponID()
     + ", shopID : " + item.getShopID()
     + ", shopname : " + item.getShopname()
     + ", shopmaplat : " + item.getShopmaplat()
     + ", shopmaplng : " + item.getShopmaplng()
     + ", shopcategory : " + item.getShopcategory()
     + ", coupontitle : " + item.getCoupontitle()
     + ", couponsaleprice : " + item.getCouponsaleprice()
     + ", couponbuyend : " + item.getCouponbuyend()
     + ", distance : " + item.getDistance()
   );*/
  }
  listData();
    }

 

이처리만 하는데 5~6분이 기본 입니다 ㅜㅜㅜ

 

너무 정보가 없고 학습이 없는 상태에서 만들려고 하니 힘드네요

 

아래는 sql 처리하는 부분 입니다.

 

 public long insertZSHOP(String tableName,String ID,String shopname,String shopmaplat,String shopmaplng,String shopcategory,String distance){
  ContentValues values = new ContentValues();
  long result = 0;
  db.beginTransaction();
  try{
   if(tableName.equals("ZSHOP")){
    values.put("ZDISTANCE", distance);
    values.put("ZMAPLAT", shopmaplat);
    values.put("ZCATEGORY", shopcategory);
    values.put("ZMAPLNG", shopmaplng);
    values.put("ZNAME", shopname);
    values.put("ZID", ID);
   }
   result = db.insert(tableName, null, values);
   db.setTransactionSuccessful();
   
  }catch (Exception e) {
   // TODO: handle exception
  }finally{
   db.endTransaction();
  }
  return result;
 }
 
 public long insertZCOUPON(String tableName,String ID,String coupontitle,String couponsaleprice,String couponbuyend,String distance){
  ContentValues values = new ContentValues();
  long result = 0;
  db.beginTransaction();
  try{
   if(tableName.equals("ZCOUPON")){
    values.put("ZBUYEND", couponbuyend);
    values.put("ZSALEPRICE", couponsaleprice);
    values.put("ZDISTANCE", distance);
    values.put("ZTITLE", coupontitle);
    values.put("ZID", ID);
    result = db.insert(tableName, null, values);
   }
   
   db.setTransactionSuccessful();
  }catch (Exception e) {
   // TODO: handle exception
  }finally{
   db.endTransaction();
  }
  return result;
 }

트랜젝션 발생시켜도 그렇고.. 참 애매하네요 .. 저것때문에 화면도 안보이고 ..

 

좋은 조언 부탁 드리겠습니다 (꾸벅)