2013년 8월 30일 금요일

메모리 관리 Tip



- (void)dealloc
{
    CCArray* children = [self children];
    for(CCNode* tempNode in children)
    {
        if(tempNode != nil && 1 < [tempNode retainCount])
        {
            while(1 < [tempNode retainCount])
            {
                [tempNode release];
            }
        }
    }
 
    [[CCTextureCache sharedTextureCache] removeAllTextures];
    [super dealloc];
}

CCLayer에 addChild가 된 모든 CCSprite 이미지들을 배열로 만들어서 해당 배열의 모든 객체의 retainCount를 확인하고 release해주는 코드이다.

확장메서드

확장 메서드의 세가지 선언 규칙
  • static 클래스 안에 구현되어야 한다.
  • static 메서드로 선언되어야 한다.
  • 항상 첫 번째 파라미터에는 본인이 포함될 타입을 지정한다. 그리고 지정된 타입 앞에는 this 키워드가 위치해야 한다.
예
public static class ExtensionString
{
    public static string LongStringLimit(this StringBuilder src, int limitLength)
    {
        string limit = src.ToString();
 
        if (src != null) src = null;
        if (limit.Length <= limitLength) return limit;
        else 
        {
            limit = limit.Substring(0, limitLength);
            return String.Format("{0}...", limit);
        }
    }
}

Execute CRUD-LINQ

ExecuteCommand : 요청할 쿼리문과 쿼리문에 적용할 파라미터를 전달받아 실행하고 실행 결과를 int 타입으로 리턴한다.
                                이 값은 쿼리문이 실행된 이후에 영향받은 데이터 건수를 가진다.

Select


IEnumerable<Products> result = north.ExecuteQuery<Products>
            ("Select * from products where unitprice >= {0}", 100);
foreach(Products prod in result)
{
    Console.WriteLIne("ProductID : {0}, Unit Price : {1}", 
                      prod.ProductID, 
                      prod.UnitPrice);
}

Insert, Update, Delete

public void ExecuteCommandTest()
{
    const string insertProd = "insert into products values({0}, {1}, {3}, {4}, {5})";
    const string updateProd = "update products set UnitPrice = UnitPrice + 2.00 where UnitPrice >= {0}";
    const string deleteProd = "delete products where unitprice >= {0}";
 
    try
    {
        int insertAffectedCnt = north.ExecuteCommand(insertProd, "Test Prod",
                                                     10,         1,
                                                     "31 boxes", 7.00,
                                                     61);
        int updateAffectedCnt = north.ExecuteCommand(updateProd, 100);
        int deleteAffectedCnt = north.ExecuteCommand(deleteProd, 100);
  
        //변경 사항을 데이터베이스에 반영
        north.SubmitChanges();
 
        //추가, 수정, 삭제 처리 후 영향받은 데이터 건수 체크
        Console.WriteLine("추가된 건 수 : {0} 수정된 건 수 : {1} 삭제된 건 수 : {2}",
                      insertAffectedCnt, updateAffectedCnt, deleteAffectedCnt);
    }
    catch(Exception ex){ Console.WriteLine(ex.Message); }
}