Zusammenfassung der Ressource
Specialized Topics
- XML Parsing
- What is XML
- HTML-like language (XHTML is
application of XML)
- Custom tags instead
of predefined
- Strict grammar
- Standard format for
data interchange
- <messages> <message
index="1"
epoch="1318236815692"
from="paul.devrieze@gmail.com">
<h1>Welcome to Programming
2</h1> </message>
</messages>
- Structure
- What in computer science
is called a tree
- what is a tree?
- One root, has any number
of children.
- Every child has one parent.
- Children can have any
number of children
- A child without children is called a leaf
- The elements of a tree are called nodes
- Parsing /chat/messages
- parseMessages(XMLPullParser
in,...)
- On END_DOCUMENT return the
stored list of messages
- On START_TAG (assuming <messages>) call
parseMessagesRoot(in,...). Store resulting
List<Message> as local variable
- Reading XML docs
- standardized parsers that do
much of the heavy lifting
- DOM - Document Object Model (builds a
tree for your in memory)
- parsing depends on recursion
- SAX - Simple API for XML (Invokes
methods in handler during parsing,
push based)
- STAX - STreaming API for XML
(Ask for the next event, pull based)
- Fast + strightfoward
- parsing depends on recursion
- Android does not have standard STAX, but
does have XMLPullParser (slightly different
API, same principle)
- XMLPullParser approach
- Ask for next() node type (skips comments)
- Have a loop containing a Switch on the type. Handle
START_TAG different from TEXT, END_TAG and
END_DOCUMENT.
- Handling START_TAG by separate
method that returns when it sees an
END_TAG
- Stop the loop on
END_TAG or
END_DOCUMENT
(remember a return stops a
loop as well
- Revision Control
- Why
- Collaboration
- Keeping track of versions
- Roll-backs
- Managing multiple releases
- Terminology
- Repository – The
database storing
changes with when
and by whom
- Working copy – A directory with
a checkout of the repository
where the work happens
- Checkout – The process of getting
a particular version out of the
repository
- Update – Getting the latest version
into the working copy
- Commit – Send a set of
changes to the repository
- VCS Operations
- Create
- A repository is a
database. It contains
more than just the files, it
maintains a history.
- The database must be
created before things can
be added
- Version control databases are
generally add-only. Old
revisions (and the files) don't
get deleted
- Checkout
- Load a revision (possibly the
latest) from the repository into
the checkout dir
- Private working copy means
multiple people can do work
without conflict
- Commit
- Add a change from the working
copy to the repository.
- The comment matters, it allows
you to find back things.
- The repository can be configured
to check code before commit, or
to send out mails afterwards
- Update
- Get the last changes from the
repository
- Add
- Signal in the working copy
that the file should be added
to the repository
- Becomes part of the
pending changeset
- Ignore
- Some files are
auto generated
- These files often
change often.
- They can be
recreated on
demand
- Most revision control
systems can be told to
ignore patterns of file and
directory names
- When ignored, files are
not proposed for adding
to the repository.
- Delete
- Delete file from future
revisions
- Generally removes file from
working copy immediately,
from repository on commit
- File is not
actually gone
- Subversion Trunk
- Threads
- Multiprocessing
- Multiple programmes can run
concurrently on a computer
- Supported by multiple
cpu's or by dividing up
a CPU into timeslices
- Each program / task has
its own memory space, file
descriptors etc
- Inter Process Communication
- To allow collaboration
between programmes Inter
Process Communication is
needed
- Using pipes (virtual files)
- Using RPC middleware
- Using regular files
- Multithreading
- Multiprocessing completely
separates instances, multi
threading shares most resources
- Multi threading allows shared memory.
Objects in one thread are present and
shared with objects in another thread.
- Java has native Thread support
- Why use multithreading
- Do computation in background
keeping UI responsive
- Handling (slow) I/O while
keeping the processor occupied
- Programming model is more
straightforward than
asynchronous I/O
- Utilizing multiple CPU(s)
and cores
- Issues with multithreading
- What happens on which thread?
- How to stop threads to step
on each other's feet.
- Deadlocks (infinite waiting)
- Race conditions
(inconsistent data)
- Thread synchronization
- Allows structured
communication between threads
- Java has synchronized
keyword (for method, or
block of code)
- Hard to get right
- Android and Java have utility
classes to make this easier
- Thread in Java
- Thread class allows starting of a thread
- The thread code is in a Runnable (Thread is a Runnable).
End of the run() methods ends the thread
- Communicating between threads
- Shared memory (either
synchronized or volatile
variables)
- Pipes – virtual files that
allow passing of data
- Message passing
- Thread.sleep(), Object.wait(),
Thread.interrupt() - Poke the
thread for attention (timing not
guaranteed)
- Shared memory
- Multiple readers at the
same time is fine
- Multiple (blind) writers
can be fine
- Mixing read-only with
write-only can be fine
- Most writing needs
reading (adding 1 to a
value)
- Unfortunately a modern programming
language is a leaky abstraction in the face of
multithreading (you want to know about
hardware)
- Use synchronization.
- Synchronization needs an
object to synchronize on.
- All readers and writers
need to be synchronized to
the same object
- Immutable objects (only final
attributes) such as Strings don't
need synchronization.
- Pipes / files
- Allow passing of
“arbitrary format” byte
streams.
- Very basic primitive
(OS based)
- Files are disk based and use
OS based synchronization
- Pipes are memory based but
also OS based.
- Message passing
- Each thread has a
message queue
- Messages may
have a return
address
- Threads have a
message pump
(event loop)
- Events are
processed
sequentially
- A special message can
be used to stop loop
- Used for processing GUI
events from the OS
- Databases
- Why databases?
- To store and retrieve data in a
permanent fashion
- Databases abstract away the
problem of data storage, caching
etc.
- Databases ensure consistency
- Complex structures are hard
to store another way efficiently
- Handles optimization and
indexing
- What databases
- Android: SQLiteDatabase
(using sqlite)
- Java: JDBC
- Interface for connecting
with many databases
- Database independent
- Extensible
- Not really straightforward
- How databases?
- Connect to a database
- Issue query / execute SQL statement
- Retrieve results
- Close connection
- Connecting to a database
- You need to declare the
database to use
- Embedded: Open file, initialise
memory data structures
- External: Authenticate,
allow server to prepare
- Connecting Android/SQLite
- SQLiteDatabase db;
db=SQLiteDatabase.openDatabase
("database.db", null,
OPEN_READONLY);
- Best way: extend
SQLiteOpenHelper
- Connect JDBC
- final String database=
"jdbc:mysql://localhost:3306/"+ "mydb";
Connection db; db = DriverManager
.getConnection(database, "myuser",
"secretpassword");
- Issue query / execute
SQL statement
- Query types
- Insert (Create)
- Query (Read)
- Update (Update)
- Delete (Delete)
- Frequent queries can be "prepared"
- Returns a "Cursor" for
reading results
- Why a "cursor"
- Databases can be big
- Result sets can be really big
- Not all results can be
stored/transferred in memory
- User might want to process
results sequentially
- Query Android/SQLite
- static final String SQL="SELECT *
FROM messages WHERE index=?";
Cursor result; result =
db.rawQuery(SQL, new String[]{
Long.toString(index)});
- Query JDBC
- static final String SQL="SELECT *
FROM messages WHERE index=?";
PreparedStatement stmt =
db.prepareStatement(SQL); stmt.setInt(0,
index); ResultSet result; result =
stmt.executeQuery();
- Retrieve results
- Row by Row
- Generic, so "ugly"
- Can tell you columns names
- If you use wildcards, retrieve (and
cache) column indices
- Don't forget to close result when finished
- Close connection
- Release resources
belonging to
connection
- Must be called for
each connection
- Database resources are
expensive, reuse them, but
close them when not needed
for long time db.close();
- Issues with databases
- Used often to store
user data
- SQL is a language
- Do not use string
concatenation for
variable parts
- This is slower,
database doesn't know
it's the same query
- This can be insecure (if the data
is not from the program)
- Android Cursors
- Like ArrayAdapter,
there is a
CursorAdapter
- CursorAdapter can
use any Cursor
- SQLiteCursors
are Cursors